Skip to content

Commit

Permalink
Refactor: simplify tests by extracting common code.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielf committed Feb 19, 2016
1 parent c0df79d commit bc7cb02
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions gnock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ var _ = Describe("gnock", func() {

req := NewRequest("GET", "http://example.com/", nil)

res, err := transport.RoundTrip(req)
Expect(err).ToNot(HaveOccurred())
res := MustRoundTrip(transport, req)
Expect(res.StatusCode).To(Equal(200))
Expect(toString(res.Body)).To(Equal("Hello, World!"))
})
Expand All @@ -39,12 +38,10 @@ var _ = Describe("gnock", func() {

req := NewRequest("GET", "http://example.com/", nil)

res, err := transport.RoundTrip(req)
Expect(err).ToNot(HaveOccurred())
res := MustRoundTrip(transport, req)
Expect(toString(res.Body)).To(Equal("Response 1"))

res, err = transport.RoundTrip(req)
Expect(err).ToNot(HaveOccurred())
res = MustRoundTrip(transport, req)
Expect(toString(res.Body)).To(Equal("Response 2"))
})
It("fakes responses for POST, PUT, OPTIONS, DELETE requests", func() {
Expand All @@ -59,27 +56,19 @@ var _ = Describe("gnock", func() {
Reply("delete")

req := NewRequest("POST", "http://example.com/", nil)

res, err := transport.RoundTrip(req)
Expect(err).ToNot(HaveOccurred())
res := MustRoundTrip(transport, req)
Expect(toString(res.Body)).To(Equal("post"))

req = NewRequest("PUT", "http://example.com/", nil)

res, err = transport.RoundTrip(req)
Expect(err).ToNot(HaveOccurred())
res = MustRoundTrip(transport, req)
Expect(toString(res.Body)).To(Equal("put"))

req = NewRequest("OPTIONS", "http://example.com/", nil)

res, err = transport.RoundTrip(req)
Expect(err).ToNot(HaveOccurred())
res = MustRoundTrip(transport, req)
Expect(toString(res.Body)).To(Equal("options"))

req = NewRequest("DELETE", "http://example.com/", nil)

res, err = transport.RoundTrip(req)
Expect(err).ToNot(HaveOccurred())
res = MustRoundTrip(transport, req)
Expect(toString(res.Body)).To(Equal("delete"))
})
It("intercepts custom HTTP methods", func() {
Expand All @@ -89,8 +78,7 @@ var _ = Describe("gnock", func() {

req := NewRequest("PROPFIND", "http://example.com/", nil)

res, err := transport.RoundTrip(req)
Expect(err).ToNot(HaveOccurred())
res := MustRoundTrip(transport, req)
Expect(res.StatusCode).To(Equal(200))
Expect(toString(res.Body)).To(Equal(`<?xml version="1.0" encoding="utf-8" ?>`))
})
Expand All @@ -110,8 +98,7 @@ var _ = Describe("gnock", func() {

req := NewRequest("GET", "http://example.com/", nil)

_, err := transport.RoundTrip(req)
Expect(err).ToNot(HaveOccurred())
MustRoundTrip(transport, req)

Expect(func() {
transport.RoundTrip(req)
Expand All @@ -126,8 +113,7 @@ var _ = Describe("gnock", func() {
req := NewRequest("GET", "http://example.com/", nil)

for i := 0; i < 4; i++ {
_, err := transport.RoundTrip(req)
Expect(err).ToNot(HaveOccurred())
MustRoundTrip(transport, req)
}

Expect(func() {
Expand All @@ -142,6 +128,12 @@ func NewRequest(method, url string, body io.Reader) *http.Request {
return req
}

func MustRoundTrip(transport http.RoundTripper, req *http.Request) *http.Response {
res, err := transport.RoundTrip(req)
Expect(err).ToNot(HaveOccurred())
return res
}

func toString(reader io.Reader) string {
data, err := ioutil.ReadAll(reader)
Expect(err).ToNot(HaveOccurred())
Expand Down

0 comments on commit bc7cb02

Please sign in to comment.