Files
superherohub/internal/router/router_test.go

173 lines
4.6 KiB
Go

package router
import (
"encoding/json"
"github.com/eslider/superherohub/pkg/deesee"
"github.com/eslider/superherohub/pkg/deesee/storage"
"github.com/eslider/superherohub/pkg/file"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// TestWebserviceGetSuperHeros tests the HandleWebserviceRequest function
func TestWebserviceGetSuperHeros(t *testing.T) {
path, err := file.GetModRootPath()
if err != nil {
t.Fatalf("Error getting module root path: %s", err)
}
// Load heros
heros, err := storage.NewJson(path + "/data/heros.json")
if err != nil {
t.Fatalf("Error loading heros: %heros", err)
}
// Create router
router := New(heros, 5, nil)
// Test cases
for _, tc := range []struct {
name string // Test name
url string // URL to test
encrypted bool // Encrypted identities
filterBy *[]string // Superpowers to filter by
}{
{"Test retrieve all superheroes", "/superheroes", false, nil},
{"Test retrieve all encrypted identities", "/superheroes?encrypted=true", true, nil},
{"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"}},
} {
// Run test case
t.Run(tc.name, func(t *testing.T) {
// Create request
req, err := http.NewRequest(http.MethodGet, tc.url, strings.NewReader(""))
if err != nil {
t.Fatal("Error creating request: ", err)
}
// 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 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)
}
}
}
}
})
}
}
// TestWebserviceStoreSuperhero tests the HandleWebserviceRequest function
// TODO: Test invalid storing data (e.g. invalid JSON)
func TestWebserviceStoreSuperhero(t *testing.T) {
// Load heros
path, err := file.GetModRootPath()
if err != nil {
t.Fatalf("Error getting module root path: %s", err)
}
// Load heros
heros, err := storage.NewJson(path + "/data/heros.json")
if err != nil {
t.Fatalf("Error loading heros: %heros", err)
}
// Create router
router := New(heros, 5, nil)
// Test with valid domain model
req, err := http.NewRequest(http.MethodGet, "/superheroes", strings.NewReader(`{
"name": "ironman",
"identity": {
"firstName": "tony",
"lastName": "stark"
},
"birthday": "1970-05-29",
"superpowers": [
"intelligence",
"flight"
]
}`))
if err != nil {
t.Fatal("Error creating request: ", err)
}
// Create response recorder
rec := httptest.NewRecorder()
router.StoreSuperHero(rec, req)
res := rec.Result()
// Test status code
if res.StatusCode != http.StatusCreated {
t.Errorf("Expected status code %d, got %d", http.StatusCreated, 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"))
}
}