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)
|
router := api.NewRouter(heros, 5, nil)
|
||||||
|
|
||||||
// Test cases
|
// Test cases
|
||||||
for _, tt := range []struct {
|
for _, tc := range []struct {
|
||||||
name string // Test name
|
name string // Test name
|
||||||
url string // URL to test
|
url string // URL to test
|
||||||
encrypted bool // Encrypted identities
|
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)", "/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 retrieve superheroes that match given superpower(s) with encrypted identities", "/superheroes?superpowers=healing&encrypted=true", true, &[]string{"healing"}},
|
||||||
} {
|
} {
|
||||||
// Test with valid domain model
|
// Run test case
|
||||||
req, err := http.NewRequest(http.MethodGet, tt.url, strings.NewReader(""))
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
if err != nil {
|
|
||||||
t.Fatal("Error creating request: ", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create response recorder
|
// Create request
|
||||||
rec := httptest.NewRecorder()
|
req, err := http.NewRequest(http.MethodGet, tc.url, strings.NewReader(""))
|
||||||
router.GetSuperHeroes(rec, req)
|
if err != nil {
|
||||||
res := rec.Result()
|
t.Fatal("Error creating request: ", err)
|
||||||
|
|
||||||
// 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 encrypted identities
|
// Create response recorder
|
||||||
if tt.encrypted {
|
rec := httptest.NewRecorder()
|
||||||
if hero.Name == "superman" && (hero.Identity.FirstName != "hqfwp" || hero.Identity.LastName != "pjsy") {
|
router.GetSuperHeroes(rec, req)
|
||||||
t.Errorf("Expected encoded superman identity")
|
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
|
// Test encrypted identities
|
||||||
if hero.SuperPowers != nil {
|
if tc.encrypted {
|
||||||
for _, gotPower := range *hero.SuperPowers {
|
if hero.Name == "superman" && (hero.Identity.FirstName != "hqfwp" || hero.Identity.LastName != "pjsy") {
|
||||||
if !deesee.IsAcceptable(gotPower) {
|
t.Errorf("Expected encoded superman identity")
|
||||||
t.Errorf("Expected superpower %s to be acceptable", gotPower)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
// TestWebserviceStoreSuperhero tests the HandleWebserviceRequest function
|
||||||
// TODO: Test invalid domain models
|
// TODO: Test invalid storing data (e.g. invalid JSON)
|
||||||
func TestWebserviceStoreSuperhero(t *testing.T) {
|
func TestWebserviceStoreSuperhero(t *testing.T) {
|
||||||
// Load heros
|
// Load heros
|
||||||
heros, err := deesee.Load(dataPath)
|
heros, err := deesee.Load(dataPath)
|
||||||
|
|||||||
Reference in New Issue
Block a user