|
| 1 | +package server_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/suite" |
| 11 | +) |
| 12 | + |
| 13 | +func GetResponse(method string, path string, body io.Reader) (*http.Response, error) { |
| 14 | + req, err := http.NewRequest(method, path, body) |
| 15 | + if err != nil { |
| 16 | + return nil, fmt.Errorf("it should be possible to build a request; %s", err.Error()) |
| 17 | + } |
| 18 | + |
| 19 | + cli := &http.Client{} |
| 20 | + resp, err := cli.Do(req) |
| 21 | + if err != nil { |
| 22 | + return nil, fmt.Errorf("the server should answer with a response; %s", err.Error()) |
| 23 | + } |
| 24 | + |
| 25 | + return resp, nil |
| 26 | +} |
| 27 | + |
| 28 | +type ClientTestSuite struct { |
| 29 | + suite.Suite |
| 30 | +} |
| 31 | + |
| 32 | +func (c *ClientTestSuite) AssertResponseBody(resp *http.Response, expectedContent string, msg string) { |
| 33 | + c.Require().NotNil(resp, "the response body should not be nil") |
| 34 | + respBody, err := ioutil.ReadAll(resp.Body) |
| 35 | + c.Require().Nil(err, "the response body should be readable") |
| 36 | + |
| 37 | + defer resp.Body.Close() |
| 38 | + c.Equal(expectedContent, string(respBody), msg) |
| 39 | +} |
| 40 | + |
| 41 | +func (c *ClientTestSuite) AssertResponseStatus(resp *http.Response, expectedStatus int, msg string) { |
| 42 | + c.Require().NotNil(resp, "the response body should not be nil") |
| 43 | + c.Equal(expectedStatus, resp.StatusCode, fmt.Sprintf("status should be %d; %s", expectedStatus, msg)) |
| 44 | +} |
| 45 | + |
| 46 | +func (c *ClientTestSuite) AssertResponseBodyStatus(resp *http.Response, expectedStatus int, expectedContent string, msg string) { |
| 47 | + c.AssertResponseBody(resp, expectedContent, msg) |
| 48 | + c.AssertResponseStatus(resp, expectedStatus, "") |
| 49 | +} |
| 50 | + |
| 51 | +type mockDB struct{} |
| 52 | + |
| 53 | +func (db *mockDB) Close() error { |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func (db *mockDB) Query(query string, args ...interface{}) (*sql.Rows, error) { |
| 58 | + return nil, nil |
| 59 | +} |
0 commit comments