Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "io"
- "net/http"
- "net/http/httptest"
- "testing"
- )
- func mockServerAndResponse(body string, statusCode int) *httptest.Server {
- return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- w.WriteHeader(statusCode)
- io.WriteString(w, body)
- }))
- }
- func TestHandlingOfHttpNotFoundCode(t *testing.T) {
- t.Run(`Test that when the external API returns a HTTP 404,
- a proper error message is returned to the user`, func(t *testing.T) {
- server := mockServerAndResponse("", http.StatusNotFound)
- expectedErrorMessage := "what you were looking for, could not be found"
- expectedErrorCode := "HTTP_404"
- _, err := GetSomething(server.URL)
- actualErrorMessage := err.Error()
- actualErrorCode := err.ErrorCode
- if expectedErrorMessage != actualErrorMessage {
- t.Errorf("Expected %s, but got %s", expectedErrorMessage, actualErrorMessage)
- }
- if expectedErrorCode != actualErrorCode {
- t.Errorf("Expected %s, but got %s", expectedErrorMessage, actualErrorCode)
- }
- })
- t.Run(`Test that when using a client and calling the external API returns a HTTP 404,
- a proper error message is returned to the user`, func(t *testing.T) {
- server := mockServerAndResponse("", http.StatusNotFound)
- expectedErrorMessage := "what you were looking for, could not be found"
- expectedErrorCode := "HTTP_404"
- _, err := GetSomethingWithClient(server.Client(), server.URL)
- actualErrorMessage := err.Error()
- actualErrorCode := err.ErrorCode
- if expectedErrorMessage != actualErrorMessage {
- t.Errorf("Expected %s, but got %s", expectedErrorMessage, actualErrorMessage)
- }
- if expectedErrorCode != actualErrorCode {
- t.Errorf("Expected %s, but got %s", expectedErrorMessage, actualErrorCode)
- }
- })
- }
- //Code
- package main
- import (
- "fmt"
- "log"
- "net/http"
- )
- type ClientError struct {
- message string
- ErrorCode string
- }
- type ClientResponse struct {
- message string
- }
- func (e *ClientError) Error() string {
- return fmt.Sprintf("%s: %s", e.message, e.ErrorCode)
- }
- func GetSomething(url string) (*ClientResponse, *ClientError) {
- client := &http.Client{}
- resp, err := client.Get(url)
- if err != nil {
- return nil, &ClientError{"unknown client error", "HTTP_500"}
- }
- if resp.StatusCode == 404 {
- return nil, &ClientError{"what you were looking for, could not be found", "HTTP_404"}
- }
- return &ClientResponse{
- message: "Success",
- }, nil
- }
- func GetSomethingWithClient(client *http.Client, url string) (*ClientResponse, *ClientError) {
- resp, err := client.Get(url)
- if err != nil {
- return nil, &ClientError{"unknown client error", "HTTP_500"}
- }
- if resp.StatusCode == 404 {
- return nil, &ClientError{"what you were looking for, could not be found", "HTTP_404"}
- }
- return &ClientResponse{
- message: "Success",
- }, nil
- }
- func main() {
- resp, err := GetSomething("http:localhost:8080/awesome")
- if err != nil {
- fmt.Printf("Your attempts have failed because: %s", err.Error())
- log.Fatal(err)
- }
- client := &http.Client{}
- resp, err = GetSomethingWithClient(client, "http:localhost:8080/awesome")
- if err != nil {
- fmt.Printf("Your attempts using client have failed because: %s", err.Error())
- log.Fatal(err)
- }
- fmt.Printf("Your attempts have succeeded with: %s", resp)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement