Skip to content

Commit 345e890

Browse files
committed
Add a basic integration test
Signed-off-by: David Pordomingo <[email protected]>
1 parent 86e1d6b commit 345e890

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

server/common_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

server/router_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package server_test
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
10+
"github.com/src-d/gitbase-playground/server"
11+
"github.com/src-d/gitbase-playground/server/handler"
12+
"github.com/src-d/gitbase-playground/server/service"
13+
14+
"github.com/stretchr/testify/suite"
15+
)
16+
17+
func TestRouterTestSuite(t *testing.T) {
18+
suite.Run(t, new(RouterTestSuite))
19+
}
20+
21+
type RouterTestSuite struct {
22+
ClientTestSuite
23+
router http.Handler
24+
server *httptest.Server
25+
db service.SQLDB
26+
}
27+
28+
const version = "test-version"
29+
30+
func (s *RouterTestSuite) SetupSuite() {
31+
logger := service.NewLogger("dev")
32+
staticHandler := &handler.Static{}
33+
s.db = &mockDB{}
34+
s.router = server.Router(
35+
logger,
36+
staticHandler,
37+
version,
38+
s.db,
39+
)
40+
}
41+
42+
func (s *RouterTestSuite) SetupTest() {
43+
s.server = httptest.NewServer(s.router)
44+
}
45+
46+
func (s *RouterTestSuite) TearDownTest() {
47+
s.server.Close()
48+
}
49+
50+
func (s *RouterTestSuite) TearDownSuite() {
51+
s.db.Close()
52+
}
53+
54+
func (s *RouterTestSuite) GetResponse(method string, path string, body io.Reader) *http.Response {
55+
url := s.server.URL + path
56+
response, err := GetResponse(method, url, body)
57+
if err != nil {
58+
s.Fail(err.Error())
59+
}
60+
61+
return response
62+
}
63+
64+
func (s *RouterTestSuite) TestVersion() {
65+
expectedVersion := fmt.Sprintf(`{"status":200,"data":{"version":"%s"}}`, version)
66+
response := s.GetResponse("GET", "/version", nil)
67+
s.AssertResponseBodyStatus(response, 200, expectedVersion, "version should be served")
68+
}

0 commit comments

Comments
 (0)