示例#1
0
func TestGetInvalidUser(t *testing.T) {
	dbmap := initDbMap()
	defer dropAndClose(dbmap)

	code, _, response, err := getUser(
		mocking.URL(mux, "GET", "/user/99"),
		mocking.Header(nil),
		nil,
	)
	log.Println(err)
	if err.Error() != "User not found" {
		t.Errorf("expected User not found error, got %v", err.Error())
		t.Fatal(err)
	}

	if code != http.StatusNotFound {
		t.Errorf("expected StatusNotFound, got %v", code)
		t.Fatal(code)
	}

	if response != nil {
		t.Errorf("expected 'Invalid user' status, got %v", response.Status)
		t.Fatal(response.Status)
	}
}
示例#2
0
func TestUpdate(t *testing.T) {
	s, _, rs, err := update(
		mocking.URL(hMux, "POST", "http://example.com/1.0/stuff/ID"),
		mocking.Header(nil),
		&MyRequest{"ID", "STUFF"},
	)
	if nil != err {
		t.Fatal(err)
	}
	if http.StatusAccepted != s {
		t.Fatal(s)
	}
	if "ID" != rs.ID || "STUFF" != rs.Stuff {
		t.Fatal(rs)
	}
}
示例#3
0
func TestGet(t *testing.T) {
	s, _, rs, err := get(
		mocking.URL(hMux, "GET", "http://example.com/1.0/stuff/ID"),
		mocking.Header(nil),
		nil,
	)
	if nil != err {
		t.Fatal(err)
	}
	if http.StatusOK != s {
		t.Fatal(s)
	}
	if "ID" != rs.ID || "STUFF" != rs.Stuff {
		t.Fatal(rs)
	}
}
示例#4
0
func TestUpdate(t *testing.T) {
	code, _, response, err := update(
		mocking.URL(hMux, "POST", "http://example.com/1.0/stuff/ID"),
		mocking.Header(nil),
		&MyRequest{"ID", "STUFF"},
	)
	if nil != err {
		t.Fatal(err)
	}
	if http.StatusAccepted != code {
		t.Fatal(code)
	}
	if "ID" != response.ID || "STUFF" != response.Stuff {
		t.Fatal(response)
	}
}
示例#5
0
func TestGet(t *testing.T) {
	code, _, response, err := get(
		mocking.URL(hMux, "GET", "http://example.com/1.0/stuff/ID"),
		mocking.Header(nil),
		nil,
	)
	if nil != err {
		t.Fatal(err)
	}
	if http.StatusOK != code {
		t.Fatal(code)
	}
	if "ID" != response.ID || "STUFF" != response.Stuff {
		t.Fatal(response)
	}
}
示例#6
0
func TestCreate(t *testing.T) {
	s, h, rs, err := create(
		mocking.URL(hMux, "POST", "http://example.com/1.0/stuff"),
		mocking.Header(nil),
		&MyRequest{"ID", "STUFF"},
	)
	if nil != err {
		t.Fatal(err)
	}
	if http.StatusCreated != s {
		t.Fatal(s)
	}
	if "http://example.com/1.0/stuff/ID" != h.Get("Content-Location") {
		t.Fatal(h)
	}
	if "ID" != rs.ID || "STUFF" != rs.Stuff {
		t.Fatal(rs)
	}
}
示例#7
0
func TestCreate(t *testing.T) {
	code, header, response, err := create(
		mocking.URL(hMux, "POST", "http://example.com/1.0/stuff"),
		mocking.Header(nil),
		&MyRequest{"ID", "STUFF"},
	)
	if nil != err {
		t.Fatal(err)
	}
	if http.StatusCreated != code {
		t.Fatal(code)
	}
	if "http://example.com/1.0/stuff/ID" != header.Get("Content-Location") {
		t.Fatal(header)
	}
	if "ID" != response.ID || "STUFF" != response.Stuff {
		t.Fatal(response)
	}
}
示例#8
0
func TestGetUser(t *testing.T) {
	dbmap := initDbMap()
	defer dropAndClose(dbmap)

	dev := newDevOrFatal(t, "Bob", "Jones", "killer_bob", "unavailable")
	dev.Save()

	code, _, response, err := getUser(
		mocking.URL(mux, "GET", "/user/"+strconv.FormatInt(dev.ID, 10)),
		mocking.Header(nil),
		nil,
	)

	if err != nil {
		t.Fatal(err)
	}
	if code != http.StatusOK {
		t.Fatal(code)
	}
	if response.FirstName != dev.FirstName {
		t.Errorf("expected Bob, got %v", response.FirstName)
		t.Fatal(response)
	}
	if response.LastName != dev.LastName {
		t.Errorf("expected %v, got %v", dev.LastName, response.LastName)
		t.Fatal(response)
	}
	if response.GithubID != dev.GithubID {
		t.Errorf("expected %v, got %v", dev.GithubID, response.GithubID)
		t.Fatal(response)
	}
	if response.Status != dev.Status {
		t.Errorf("expected %v, got %v", dev.Status, response.Status)
		t.Fatal(response)
	}
}