Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "io"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. )
  9.  
  10. func mockServerAndResponse(body string, statusCode int) *httptest.Server {
  11. return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  12. w.WriteHeader(statusCode)
  13. io.WriteString(w, body)
  14. }))
  15. }
  16.  
  17. func TestHandlingOfHttpNotFoundCode(t *testing.T) {
  18. t.Run(`Test that when the external API returns a HTTP 404,
  19. a proper error message is returned to the user`, func(t *testing.T) {
  20. server := mockServerAndResponse("", http.StatusNotFound)
  21.  
  22. expectedErrorMessage := "what you were looking for, could not be found"
  23. expectedErrorCode := "HTTP_404"
  24.  
  25. _, err := GetSomething(server.URL)
  26.  
  27. actualErrorMessage := err.Error()
  28. actualErrorCode := err.ErrorCode
  29.  
  30. if expectedErrorMessage != actualErrorMessage {
  31. t.Errorf("Expected %s, but got %s", expectedErrorMessage, actualErrorMessage)
  32. }
  33.  
  34. if expectedErrorCode != actualErrorCode {
  35. t.Errorf("Expected %s, but got %s", expectedErrorMessage, actualErrorCode)
  36. }
  37. })
  38.  
  39. t.Run(`Test that when using a client and calling the external API returns a HTTP 404,
  40. a proper error message is returned to the user`, func(t *testing.T) {
  41. server := mockServerAndResponse("", http.StatusNotFound)
  42.  
  43. expectedErrorMessage := "what you were looking for, could not be found"
  44. expectedErrorCode := "HTTP_404"
  45.  
  46. _, err := GetSomethingWithClient(server.Client(), server.URL)
  47.  
  48. actualErrorMessage := err.Error()
  49. actualErrorCode := err.ErrorCode
  50.  
  51. if expectedErrorMessage != actualErrorMessage {
  52. t.Errorf("Expected %s, but got %s", expectedErrorMessage, actualErrorMessage)
  53. }
  54.  
  55. if expectedErrorCode != actualErrorCode {
  56. t.Errorf("Expected %s, but got %s", expectedErrorMessage, actualErrorCode)
  57. }
  58. })
  59. }
  60.  
  61.  
  62. //Code
  63.  
  64. package main
  65.  
  66. import (
  67. "fmt"
  68. "log"
  69. "net/http"
  70. )
  71.  
  72. type ClientError struct {
  73. message string
  74. ErrorCode string
  75. }
  76.  
  77. type ClientResponse struct {
  78. message string
  79. }
  80.  
  81. func (e *ClientError) Error() string {
  82. return fmt.Sprintf("%s: %s", e.message, e.ErrorCode)
  83. }
  84.  
  85. func GetSomething(url string) (*ClientResponse, *ClientError) {
  86. client := &http.Client{}
  87. resp, err := client.Get(url)
  88. if err != nil {
  89. return nil, &ClientError{"unknown client error", "HTTP_500"}
  90. }
  91.  
  92. if resp.StatusCode == 404 {
  93. return nil, &ClientError{"what you were looking for, could not be found", "HTTP_404"}
  94. }
  95.  
  96. return &ClientResponse{
  97. message: "Success",
  98. }, nil
  99. }
  100.  
  101. func GetSomethingWithClient(client *http.Client, url string) (*ClientResponse, *ClientError) {
  102. resp, err := client.Get(url)
  103. if err != nil {
  104. return nil, &ClientError{"unknown client error", "HTTP_500"}
  105. }
  106.  
  107. if resp.StatusCode == 404 {
  108. return nil, &ClientError{"what you were looking for, could not be found", "HTTP_404"}
  109. }
  110.  
  111. return &ClientResponse{
  112. message: "Success",
  113. }, nil
  114. }
  115.  
  116. func main() {
  117. resp, err := GetSomething("http:localhost:8080/awesome")
  118. if err != nil {
  119. fmt.Printf("Your attempts have failed because: %s", err.Error())
  120. log.Fatal(err)
  121. }
  122.  
  123. client := &http.Client{}
  124. resp, err = GetSomethingWithClient(client, "http:localhost:8080/awesome")
  125. if err != nil {
  126. fmt.Printf("Your attempts using client have failed because: %s", err.Error())
  127. log.Fatal(err)
  128. }
  129.  
  130. fmt.Printf("Your attempts have succeeded with: %s", resp)
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement