func TestGetUsersForRoom(t *testing.T) { db.Open() defer db.Close() fmt.Println("GetRooms") // Arrange recorder := httptest.NewRecorder() request, _ := http.NewRequest("GET", "/rooms/1/users", nil) request.Header = http.Header{} // Act _, cookie := login(t) request.Header.Set("Cookie", cookie) web.AdHoc(recorder, request) result := fromJson(t, recorder.Body.Bytes()) // Assert if length := len(result.([]interface{})); length != 6 { t.Error(result) t.Fatal("incorrect length returned:", length) } for _, user := range result.([]interface{}) { fmt.Println(user) } }
func login(t *testing.T) (User, string) { recorder := httptest.NewRecorder() request, _ := http.NewRequest("POST", "/login", nil) // make request web.AdHoc(recorder, request) cookie := recorder.Header().Get("Set-Cookie") result := fromJson(t, recorder.Body.Bytes()) var data = map[string]interface{}{} data = result.(map[string]interface{}) // put the results into an struct user := new(User) user.Name = data["Name"].(string) user.Email = data["Email"].(string) user.Id = int64(data["Id"].(float64)) return *user, cookie }
func TestGetMessages(t *testing.T) { db.Open() defer db.Close() fmt.Println("GetMessages") // Arrange recorder := httptest.NewRecorder() request, _ := http.NewRequest("GET", "/rooms/1/messages", nil) request.Header = http.Header{} // Act _, cookie := login(t) request.Header.Set("Cookie", cookie) web.AdHoc(recorder, request) result := fromJson(t, recorder.Body.Bytes()) // Assert for _, message := range result.([]interface{}) { fmt.Println(message) } }
func TestSaveMessages(t *testing.T) { db.Open() defer db.Close() fmt.Println("SaveMessages") // Arrange recorder := httptest.NewRecorder() request, _ := http.NewRequest("POST", "/rooms/1/messages", nil) request.Header = http.Header{} request.Body = getNopCloser(bytes.NewBufferString("this is a new message")) // Act _, cookie := login(t) request.Header.Set("Cookie", cookie) web.AdHoc(recorder, request) result := fromJson(t, recorder.Body.Bytes()) // Assert for _, message := range result.([]interface{}) { fmt.Println(message) } }