Skip to content

Commit

Permalink
Add test that upstream actually talks to an upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Mar 3, 2015
1 parent 35eb24e commit de47c6a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ type Stats interface {
}

type Transport interface {
http.RoundTripper
RoundTrip(*http.Request) (*http.Response, error)
CancelRequest(req *http.Request)
}
21 changes: 21 additions & 0 deletions mock_Transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package templar

import "github.com/stretchr/testify/mock"

import "net/http"

type MockTransport struct {
mock.Mock
}

func (m *MockTransport) RoundTrip(_a0 *http.Request) (*http.Response, error) {
ret := m.Called(_a0)

r0 := ret.Get(0).(*http.Response)
r1 := ret.Error(1)

return r0, r1
}
func (m *MockTransport) CancelRequest(req *http.Request) {
m.Called(req)
}
26 changes: 25 additions & 1 deletion upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,33 @@ func (s slowTransport) RoundTrip(req *http.Request) (*http.Response, error) {
func (s slowTransport) CancelRequest(req *http.Request) {
}

func TestUpstreamTimeout(t *testing.T) {
func TestUpstream(t *testing.T) {
n := neko.Start(t)

n.It("sends a request to the transport", func() {
req, err := http.NewRequest("GET", "http://google.com/foo/bar", nil)
require.NoError(t, err)

res := httptest.NewRecorder()

var mockTrans MockTransport

timeout := NewUpstream(&mockTrans)

upstream := &http.Response{
Request: req,
StatusCode: 304,
Status: "304 Too Funky",
}

mockTrans.On("RoundTrip", req).Return(upstream, nil)

err = timeout.Forward(res, req)
require.NoError(t, err)

assert.Equal(t, 304, res.Code)
})

n.It("will timeout a request if requested", func() {
req, err := http.NewRequest("GET", "http://google.com/foo/bar", nil)
require.NoError(t, err)
Expand Down

0 comments on commit de47c6a

Please sign in to comment.