예제 #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)
	}
}
예제 #9
0
func TestNotFound(t *testing.T) {
	defer func() { recover() }()
	mocking.URL(hMux, "GET", "http://example.com/1.0/things")
	t.Fail()
}
예제 #10
0
func TestMethodNotAllowed(t *testing.T) {
	defer func() { recover() }()
	mocking.URL(hMux, "PUT", "http://example.com/1.0/stuff")
	t.Fail()
}