Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Stein Fletcher committed Feb 13, 2019
1 parent b5f8a90 commit d383618
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
66 changes: 34 additions & 32 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var responseDebugPrefix = fmt.Sprintf("<%s", divider)
type APITest struct {
debugEnabled bool
reporter ReportFormatter
handler http.Handler
name string
request *Request
response *Response
Expand Down Expand Up @@ -101,6 +102,12 @@ func (a *APITest) Meta(meta map[string]interface{}) *APITest {
return a
}

// Handler defines the http handler that is invoked when the test is run
func (a *APITest) Handler(handler http.Handler) *APITest {
a.handler = handler
return a
}

// Mocks is a builder method for setting the mocks
func (a *APITest) Mocks(mocks ...*Mock) *APITest {
var m []*Mock
Expand Down Expand Up @@ -150,15 +157,8 @@ func (a *APITest) Response() *Response {
return a.response
}

// Handler defines the http handler that is invoked when the test is run
func (a *APITest) Handler(handler http.Handler) *Request {
a.request.handler = handler
return a.request
}

// Request is the user defined request that will be invoked on the handler under test
type Request struct {
handler http.Handler
interceptor Intercept
method string
url string
Expand All @@ -180,54 +180,56 @@ type pair struct {
}

// Intercept is a builder method for setting the request interceptor
func (r *Request) Intercept(interceptor Intercept) *Request {
r.interceptor = interceptor
return r
func (a *APITest) Intercept(interceptor Intercept) *APITest {
a.request.interceptor = interceptor
return a
}

// Method is a builder method for setting the http method of the request
func (r *Request) Method(method string) *Request {
r.method = method
return r
}

// URL is a builder method for setting the url of the request
func (r *Request) URL(url string) *Request {
r.url = url
return r
func (a *APITest) Method(method string) *Request {
a.request.method = method
return a.request
}

// Get is a convenience method for setting the request as http.MethodGet
func (r *Request) Get(url string) *Request {
r.method = http.MethodGet
r.url = url
return r
func (a *APITest) Get(url string) *Request {
a.request.method = http.MethodGet
a.request.url = url
return a.request
}

// Post is a convenience method for setting the request as http.MethodPost
func (r *Request) Post(url string) *Request {
func (a *APITest) Post(url string) *Request {
r := a.request
r.method = http.MethodPost
r.url = url
return r
}

// Put is a convenience method for setting the request as http.MethodPut
func (r *Request) Put(url string) *Request {
func (a *APITest) Put(url string) *Request {
r := a.request
r.method = http.MethodPut
r.url = url
return r
}

// Delete is a convenience method for setting the request as http.MethodDelete
func (r *Request) Delete(url string) *Request {
r.method = http.MethodDelete
r.url = url
return r
func (a *APITest) Delete(url string) *Request {
a.request.method = http.MethodDelete
a.request.url = url
return a.request
}

// Patch is a convenience method for setting the request as http.MethodPatch
func (r *Request) Patch(url string) *Request {
r.method = http.MethodPatch
func (a *APITest) Patch(url string) *Request {
a.request.method = http.MethodPatch
a.request.url = url
return a.request
}

// URL is a builder method for setting the url of the request
func (r *Request) URL(url string) *Request {
r.url = url
return r
}
Expand Down Expand Up @@ -568,7 +570,7 @@ func (a *APITest) serveHttp(res *httptest.ResponseRecorder, req *http.Request) {
}
}()

a.request.handler.ServeHTTP(res, req)
a.handler.ServeHTTP(res, req)
}

func (a *APITest) BuildRequest() *http.Request {
Expand Down
2 changes: 1 addition & 1 deletion apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,11 @@ func TestApiTest_Intercept(t *testing.T) {

New().
Handler(handler).
Get("/hello").
Intercept(func(req *http.Request) {
req.URL.RawQuery = "a[]=xxx&a[]=yyy"
req.Header.Set("Auth-Token", req.Header.Get("authtoken"))
}).
Get("/hello").
Headers(map[string]string{"authtoken": "12345"}).
Expect(t).
Status(http.StatusOK).
Expand Down
3 changes: 2 additions & 1 deletion examples/sequence-diagrams-with-database/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestGetUser_With_Default_Report_Formatter(t *testing.T) {

apitest.New("gets the user").
Mocks(getUserMock(username)).
Report(apitest.SequenceDiagram()).
RecorderHook(RecordingHook(testDB)).
Handler(app.Router).
Get("/user").
Expand All @@ -33,7 +34,7 @@ func TestGetUser_With_Default_Report_Formatter(t *testing.T) {
Status(http.StatusOK).
Header("Content-Type", "application/json").
Body(fmt.Sprintf(`{"name": "%s", "is_contactable": true}`, username)).
Report()
End()
}

func getUserMock(username string) *apitest.Mock {
Expand Down
3 changes: 1 addition & 2 deletions examples/sequence-diagrams-with-database/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
const dbAddr = "host=localhost port=5432 user=postgres password=postgres dbname=apitest sslmode=disable"

func RecordingHook(db DB) apitest.RecorderHook {
return func(recorder *apitest.Recorder, appName string) {
return func(recorder *apitest.Recorder) {
if v, ok := db.(*recordingDB); ok {
v.recorder = recorder
v.sourceName = appName
}
}
}
Expand Down

0 comments on commit d383618

Please sign in to comment.