Skip to content

Commit

Permalink
Merge pull request #23 from gomicro/support-headers
Browse files Browse the repository at this point in the history
adding support for custom response headers per path
  • Loading branch information
dan9186 authored Apr 11, 2018
2 parents 4cfed51 + 5e00bd8 commit dbd2e06
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.2
- 1.6
- tip
before_script:
- curl -s -L -I https://gocover.io/_/github.com/gomicro/bogus
Expand Down
23 changes: 23 additions & 0 deletions bogus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,28 @@ func TestBogus(t *testing.T) {
Expect(err).NotTo(HaveOccurred())
Expect(string(body)).To(Equal("Not Found"))
})

g.It("should return unique headers per path", func() {
headers1 := map[string]string{"Content-Type": "plain/text"}
server.AddPath("/").
SetMethods("GET").
SetHeaders(headers1)

headers2 := map[string]string{"Content-Type": "application/json"}
server.AddPath("/foo/bar").
SetMethods("GET").
SetHeaders(headers2)

resp, err := http.Get("http://" + net.JoinHostPort(host, port))
Expect(err).NotTo(HaveOccurred())
defer resp.Body.Close()

Expect(resp.Header.Get("Content-Type")).To(Equal("plain/text"))

resp, err = http.Get("http://" + net.JoinHostPort(host, port) + "/foo/bar")
Expect(err).NotTo(HaveOccurred())

Expect(resp.Header.Get("Content-Type")).To(Equal("application/json"))
})
})
}
12 changes: 12 additions & 0 deletions paths/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
// Path represents an endpoint added to a bogus server and how it should respond
type Path struct {
Hits int
headers map[string]string
payload []byte
status int
methods []string
Expand All @@ -21,6 +22,13 @@ func New() *Path {
}
}

// SetHeaders sets the response headers for the path and returns the path for
// additional configuration
func (p *Path) SetHeaders(headers map[string]string) *Path {
p.headers = headers
return p
}

// SetPayload sets the response payload for the path and returns the path for
// additional configuration
func (p *Path) SetPayload(payload []byte) *Path {
Expand Down Expand Up @@ -52,6 +60,10 @@ func (p *Path) HandleRequest(w http.ResponseWriter, r *http.Request) {
payload := []byte("")
status := http.StatusForbidden

for header, value := range p.headers {
w.Header().Set(header, value)
}

if p.hasMethod(r.Method) {
p.Hits++
w.WriteHeader(p.status)
Expand Down
13 changes: 13 additions & 0 deletions paths/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ func TestBogus(t *testing.T) {
})
})

g.Describe("Setting Headers", func() {
g.It("should allow setting headers for paths", func() {
headers := map[string]string{
"Content-Type": "plain/text",
}
p := New().
SetHeaders(headers)

Expect(p.headers["Content-Type"]).To(Equal("plain/text"))
Expect(p.headers).To(Equal(headers))
})
})

g.Describe("Setting Methods", func() {
g.It("should allow setting methods for paths", func() {
p := New().
Expand Down

0 comments on commit dbd2e06

Please sign in to comment.