ghttp server can take an io.Writer
the server will write a line to the writer when each request is received
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0eec5a8..752f52d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@
- Improved `ghttp`'s behavior around failing assertions and panics:
- If a registered handler makes a failing assertion `ghttp` will return `500`.
- If a registered handler panics, `ghttp` will return `500` *and* fail the test. This is new behavior that may cause existing code to break. This code is almost certainly incorrect and creating a false positive.
+- `ghttp` servers can take an `io.Writer`. `ghttp` will write a line to the writer when each request arrives.
Bug Fixes:
- gexec: `session.Wait` now uses `EventuallyWithOffset` to get the right line number in the failure.
diff --git a/ghttp/test_server.go b/ghttp/test_server.go
index 7b4a996..fde65be 100644
--- a/ghttp/test_server.go
+++ b/ghttp/test_server.go
@@ -106,6 +106,8 @@
package ghttp
import (
+ "fmt"
+ "io"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -165,6 +167,11 @@
//Only applies if AllowUnhandledRequests is true
UnhandledRequestStatusCode int
+ //If provided, ghttp will log about each request received to the provided io.Writer
+ //Defaults to nil
+ //If you're using Ginkgo, set this to GinkgoWriter to get improved output during failures
+ Writer io.Writer
+
receivedRequests []*http.Request
requestHandlers []http.HandlerFunc
routedHandlers []routedHandler
@@ -234,6 +241,10 @@
Ω(e).Should(BeNil(), "Handler Panicked")
}()
+ if s.Writer != nil {
+ s.Writer.Write([]byte(fmt.Sprintf("GHTTP Received Request: %s - %s\n", req.Method, req.URL)))
+ }
+
s.receivedRequests = append(s.receivedRequests, req)
if routedHandler, ok := s.handlerForRoute(req.Method, req.URL.Path); ok {
s.writeLock.Unlock()
diff --git a/ghttp/test_server_test.go b/ghttp/test_server_test.go
index 25260b6..4d55b0f 100644
--- a/ghttp/test_server_test.go
+++ b/ghttp/test_server_test.go
@@ -7,6 +7,8 @@
"net/url"
"regexp"
+ "github.com/onsi/gomega/gbytes"
+
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/ghttp"
@@ -237,6 +239,24 @@
})
})
+ Describe("Logging to the Writer", func() {
+ var buf *gbytes.Buffer
+ BeforeEach(func() {
+ buf = gbytes.NewBuffer()
+ s.Writer = buf
+ s.AppendHandlers(func(w http.ResponseWriter, req *http.Request) {})
+ s.AppendHandlers(func(w http.ResponseWriter, req *http.Request) {})
+ })
+
+ It("should write to the buffer when a request comes in", func() {
+ http.Get(s.URL() + "/foo")
+ Ω(buf).Should(gbytes.Say("GHTTP Received Request: GET - /foo\n"))
+
+ http.Post(s.URL()+"/bar", "", nil)
+ Ω(buf).Should(gbytes.Say("GHTTP Received Request: POST - /bar\n"))
+ })
+ })
+
Describe("Request Handlers", func() {
Describe("VerifyRequest", func() {
BeforeEach(func() {