Refactor router test.
* Split test-cases into own runners
This commit is contained in:
132
router_test.go
132
router_test.go
@@ -22,7 +22,7 @@ func TestWebserviceGetSuperHeros(t *testing.T) {
|
||||
router := api.NewRouter(heros, 5, nil)
|
||||
|
||||
// Test cases
|
||||
for _, tt := range []struct {
|
||||
for _, tc := range []struct {
|
||||
name string // Test name
|
||||
url string // URL to test
|
||||
encrypted bool // Encrypted identities
|
||||
@@ -33,81 +33,85 @@ func TestWebserviceGetSuperHeros(t *testing.T) {
|
||||
{"Test retrieve superheroes that match given superpower(s)", "/superheroes?superpowers=healing", false, &[]string{"healing"}},
|
||||
{"Test retrieve superheroes that match given superpower(s) with encrypted identities", "/superheroes?superpowers=healing&encrypted=true", true, &[]string{"healing"}},
|
||||
} {
|
||||
// Test with valid domain model
|
||||
req, err := http.NewRequest(http.MethodGet, tt.url, strings.NewReader(""))
|
||||
if err != nil {
|
||||
t.Fatal("Error creating request: ", err)
|
||||
}
|
||||
// Run test case
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
||||
// Create response recorder
|
||||
rec := httptest.NewRecorder()
|
||||
router.GetSuperHeroes(rec, req)
|
||||
res := rec.Result()
|
||||
|
||||
// Test status code
|
||||
if res.StatusCode != http.StatusOK {
|
||||
t.Errorf("Expected status code %d, got %d", http.StatusOK, res.StatusCode)
|
||||
}
|
||||
|
||||
// Test content type
|
||||
if res.Header.Get("Content-Type") != "application/json" {
|
||||
t.Errorf("Expected content type %s, got %s", "application/json", res.Header.Get("Content-Type"))
|
||||
}
|
||||
|
||||
// Test decode response
|
||||
heros := &[]*deesee.Superhero{}
|
||||
if err = json.NewDecoder(res.Body).Decode(heros); err != nil {
|
||||
t.Fatalf("Error decoding response: %heros", err)
|
||||
}
|
||||
|
||||
// Test non-nil response
|
||||
if heros == nil {
|
||||
t.Fatalf("Expected non-nil response")
|
||||
}
|
||||
|
||||
// Test length of response
|
||||
if len(*heros) < 1 {
|
||||
t.Fatalf("Expected non-empty response")
|
||||
}
|
||||
|
||||
// Test heros entries
|
||||
for _, hero := range *heros {
|
||||
|
||||
// Test heros identities
|
||||
if hero.Identity == nil {
|
||||
t.Errorf("Expected identity to be %s, got %s", "unknown", hero.Identity)
|
||||
// Create request
|
||||
req, err := http.NewRequest(http.MethodGet, tc.url, strings.NewReader(""))
|
||||
if err != nil {
|
||||
t.Fatal("Error creating request: ", err)
|
||||
}
|
||||
|
||||
// Test encrypted identities
|
||||
if tt.encrypted {
|
||||
if hero.Name == "superman" && (hero.Identity.FirstName != "hqfwp" || hero.Identity.LastName != "pjsy") {
|
||||
t.Errorf("Expected encoded superman identity")
|
||||
// Create response recorder
|
||||
rec := httptest.NewRecorder()
|
||||
router.GetSuperHeroes(rec, req)
|
||||
res := rec.Result()
|
||||
|
||||
// Test status code
|
||||
if res.StatusCode != http.StatusOK {
|
||||
t.Errorf("Expected status code %d, got %d", http.StatusOK, res.StatusCode)
|
||||
}
|
||||
|
||||
// Test content type
|
||||
if res.Header.Get("Content-Type") != "application/json" {
|
||||
t.Errorf("Expected content type %s, got %s", "application/json", res.Header.Get("Content-Type"))
|
||||
}
|
||||
|
||||
// Test decode response
|
||||
heros := &[]*deesee.Superhero{}
|
||||
if err = json.NewDecoder(res.Body).Decode(heros); err != nil {
|
||||
t.Fatalf("Error decoding response: %heros", err)
|
||||
}
|
||||
|
||||
// Test non-nil response
|
||||
if heros == nil {
|
||||
t.Fatalf("Expected non-nil response")
|
||||
}
|
||||
|
||||
// Test length of response
|
||||
if len(*heros) < 1 {
|
||||
t.Fatalf("Expected non-empty response")
|
||||
}
|
||||
|
||||
// Test heros entries
|
||||
for _, hero := range *heros {
|
||||
|
||||
// Test heros identities
|
||||
if hero.Identity == nil {
|
||||
t.Errorf("Expected identity to be %s, got %s", "unknown", hero.Identity)
|
||||
}
|
||||
}
|
||||
|
||||
// Test superpowers
|
||||
if hero.SuperPowers != nil {
|
||||
for _, gotPower := range *hero.SuperPowers {
|
||||
if !deesee.IsAcceptable(gotPower) {
|
||||
t.Errorf("Expected superpower %s to be acceptable", gotPower)
|
||||
// Test encrypted identities
|
||||
if tc.encrypted {
|
||||
if hero.Name == "superman" && (hero.Identity.FirstName != "hqfwp" || hero.Identity.LastName != "pjsy") {
|
||||
t.Errorf("Expected encoded superman identity")
|
||||
}
|
||||
}
|
||||
|
||||
// Test superpowers
|
||||
if hero.SuperPowers != nil {
|
||||
for _, gotPower := range *hero.SuperPowers {
|
||||
if !deesee.IsAcceptable(gotPower) {
|
||||
t.Errorf("Expected superpower %s to be acceptable", gotPower)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test superpowers filter
|
||||
if tc.filterBy != nil {
|
||||
for _, wantPower := range *tc.filterBy {
|
||||
if !hero.Has(wantPower) {
|
||||
t.Errorf("Expected superpower %s to be in %s", wantPower, hero.SuperPowers)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test superpowers filter
|
||||
if tt.filterBy != nil {
|
||||
for _, wantPower := range *tt.filterBy {
|
||||
if !hero.Has(wantPower) {
|
||||
t.Errorf("Expected superpower %s to be in %s", wantPower, hero.SuperPowers)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestWebserviceStoreSuperhero tests the HandleWebserviceRequest function
|
||||
// TODO: Test invalid domain models
|
||||
// TODO: Test invalid storing data (e.g. invalid JSON)
|
||||
func TestWebserviceStoreSuperhero(t *testing.T) {
|
||||
// Load heros
|
||||
heros, err := deesee.Load(dataPath)
|
||||
|
||||
Reference in New Issue
Block a user