Пример #1
0
func TestJsonHandler(t *testing.T) {
	mux := my_app.NewMux()
	res := httptest.NewRecorder()
	req, _ := http.NewRequest("POST", "/json", strings.NewReader(`{"first_name":"Todd","last_name":"McLeod"}`))
	mux.ServeHTTP(res, req)

	if res.Code != 201 {
		t.Error("!= 201")
	}

	if res.Header().Get("Content-Type") != "application/json" {
		t.Error("application/json")
	}

	user := new(my_app.User)
	json.NewDecoder(res.Body).Decode(user)

	if user.Id != 1 {
		t.Error("!= 1")
	}

	if user.FirstName != "Tod" {
		t.Error("!= Todd")
	}

	if user.LastName != "McLeod" {
		t.Errorf("%s != McLeod", user.LastName)
	}
}
Пример #2
0
func TestWelcomeByNameHandler_WithName(t *testing.T) {
	mux := my_app.NewMux()
	res := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/hello?name=Todd", nil)
	mux.ServeHTTP(res, req)

	if res.Code != 200 {
		t.Error("!= 200")
	}

	if res.Body.String() != "Hello, Todd!" {
		t.Error("Hello, World!")
	}
}
Пример #3
0
func TestHomePageHandler(t *testing.T) {
	mux := my_app.NewMux()
	res := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/", nil)
	mux.ServeHTTP(res, req)

	if res.Code != 200 {
		t.Errorf("!= 200")
	}

	if res.Body.String() != "Hello, World!" {
		t.Errorf("Hello, World!")
	}
}
Пример #4
0
func main() {
	http.ListenAndServe(":3000", my_app.NewMux())
}