Last active
December 8, 2022 06:27
-
-
Save aliuygur/3f133acfa7477c634d3fffe9496a28ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// client_test.go | |
package rickandmorty | |
import ( | |
"io/ioutil" | |
"net/http" | |
"strings" | |
"testing" | |
) | |
type RoundTripFunc func(req *http.Request) *http.Response | |
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { | |
return f(req), nil | |
} | |
// Test4xxError tests that a 4xx error is returned as an APIError. | |
func Test4xxError(t *testing.T) { | |
hclient := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response { | |
return &http.Response{ | |
StatusCode: http.StatusBadRequest, | |
Body: ioutil.NopCloser(strings.NewReader(`{"error":"Character not found"}`)), | |
} | |
})} | |
client := Client{client: hclient, baseURL: "https://rickandmortyapi.com/api"} | |
_, err := client.GetCharacter(-1) // -1 is not a valid character ID so this should return an error | |
if err == nil { | |
t.Error("expected error, got nil") | |
} | |
apiError, ok := err.(*APIError) | |
if !ok { | |
t.Error("expected error to be of type APIError") | |
} | |
if apiError.Err != "Character not found" { | |
t.Errorf("expected error message to be Character not found, got %s", apiError.Err) | |
} | |
} | |
// Test5xxError tests that a 5xx error is returned as an APIError. | |
func Test5xxError(t *testing.T) { | |
hclient := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response { | |
return &http.Response{ | |
StatusCode: http.StatusInternalServerError, | |
Body: ioutil.NopCloser(strings.NewReader(`Internal server error`)), | |
} | |
})} | |
client := Client{client: hclient, baseURL: "https://rickandmortyapi.com/api"} | |
_, err := client.GetCharacter(1) | |
if err == nil { | |
t.Error("expected error, got nil") | |
} | |
if _, ok := err.(*APIError); ok { | |
t.Error("expected error to not be of type APIError") | |
} | |
} | |
func TestGetCharacterByID(t *testing.T) { | |
characterJSON := `{"id":2,"name":"Morty Smith","status":"Alive","species":"Human","type":"","gender":"Male","image":"https://rickandmortyapi.com/api/character/avatar/2.jpeg","url":"https://rickandmortyapi.com/api/character/2","created":"2017-11-04T18:50:21.651Z"}` | |
hclient := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response { | |
if req.URL.String() != "https://rickandmortyapi.com/api/character/2" { | |
t.Error("expected request to https://rickandmortyapi.com/api/character/2, got", req.URL.String()) | |
} | |
if req.Method != "GET" { | |
t.Error("expected request method to be GET, got", req.Method) | |
} | |
if req.Header.Get("Accept") != "application/json" { | |
t.Error("expected request Accept header to be application/json, got", req.Header.Get("Accept")) | |
} | |
if req.Header.Get("Content-Type") != "application/json" { | |
t.Error("expected request Content-Type header to be application/json, got", req.Header.Get("Content-Type")) | |
} | |
return &http.Response{ | |
StatusCode: http.StatusOK, | |
Body: ioutil.NopCloser(strings.NewReader(characterJSON)), | |
} | |
})} | |
client := Client{client: hclient, baseURL: "https://rickandmortyapi.com/api"} | |
character, err := client.GetCharacter(2) | |
if err != nil { | |
t.Errorf("unexpected error: %v", err) | |
} | |
if character.ID != 2 { | |
t.Errorf("expected character ID to be 2, got %d", character.ID) | |
} | |
if character.Name != "Morty Smith" { | |
t.Errorf("expected character name to be Morty Smith, got %s", character.Name) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment