Skip to content

Commit

Permalink
Add MaterialsHandler test
Browse files Browse the repository at this point in the history
  • Loading branch information
jfyuen committed Mar 31, 2016
1 parent 03ee029 commit 3ac4d4f
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package recycleme

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -122,3 +123,34 @@ func TestThrowAwayHandler(t *testing.T) {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
}

func TestMaterialsHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/materials/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(MaterialsHandler)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
var out map[string]Material
err = json.Unmarshal(rr.Body.Bytes(), &out)
if err != nil {
t.Fatal(err)
}
if len(out) != len(Materials) {
t.Fatalf("got different size %v vs %v", len(out), len(Materials))
}
for k, v := range out {
id, err := strconv.Atoi(k)
if err != nil {
t.Fatal(err)
}
m := Materials[id]
if v.Name != m.Name {
t.Errorf("got different values for key %v: %v vs %v", k, v.Name, m.Name)
}
}
}

0 comments on commit 3ac4d4f

Please sign in to comment.