Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Body not repeat issue resolved #57

Open
wants to merge 3 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ func TestConfig(t *testing.T) {
w.Header().Add("intercepted", "true")
w.Header().Add(headers.ContentType, mimetypes.TextPlain)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(msg))
_, err := w.Write([]byte(msg))
if err != nil {
log.Fatal(err)
}
})
}

Expand Down
5 changes: 4 additions & 1 deletion cors/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ func TestCORS(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte(msg))
_, err := w.Write([]byte(msg))
if err != nil {
log.Fatal(err)
}
}

t.Run("should allow request", func(t *testing.T) {
Expand Down
22 changes: 17 additions & 5 deletions http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package mocha

import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -100,7 +102,13 @@ func (h *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
respondError(w, r, h.evt, err)
return
}

copy, err := ioutil.ReadAll(res.Body)
if err != nil {
h.t.Logf(err.Error())
respondError(w, r, h.evt, err)
return
}
res.Body = ioutil.NopCloser(bytes.NewBuffer(copy))
// map the response using mock mappers.
mapperArgs := reply.ResponseMapperArgs{Request: r, Parameters: h.params}
for _, mapper := range res.Mappers {
Expand All @@ -127,12 +135,16 @@ func (h *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if res.Body != nil {
scanner := bufio.NewScanner(res.Body)
for scanner.Scan() {
w.Write(scanner.Bytes())
_, err := w.Write(scanner.Bytes())
if err != nil {
h.t.Logf("error writing reponse body: error=%v", err.Error())
}
}

if scanner.Err() != nil {
h.t.Logf("error writing response body: error=%v", scanner.Err())
}
res.Body = ioutil.NopCloser(bytes.NewBuffer(copy))
}

// run post actions.
Expand Down Expand Up @@ -183,7 +195,7 @@ func respondNonMatched(w http.ResponseWriter, r *http.Request, result *findResul

w.Header().Add(headers.ContentType, mimetypes.TextPlain)
w.WriteHeader(http.StatusTeapot)
w.Write([]byte(builder.String()))
_, _ = w.Write([]byte(builder.String()))
}

func respondError(w http.ResponseWriter, r *http.Request, evt *hooks.Emitter, err error) {
Expand All @@ -192,6 +204,6 @@ func respondError(w http.ResponseWriter, r *http.Request, evt *hooks.Emitter, er
w.Header().Add(headers.ContentType, mimetypes.TextPlain)
w.WriteHeader(http.StatusTeapot)

w.Write([]byte("Request did not match. An error occurred.\n"))
w.Write([]byte(fmt.Sprintf("%v", err)))
_, _ = w.Write([]byte("Request did not match. An error occurred.\n"))
_, _ = w.Write([]byte(fmt.Sprintf("%v", err)))
}
5 changes: 4 additions & 1 deletion internal/middleware/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ func TestMiddlewaresComposition(t *testing.T) {
w.Header().Add("x-two", r.Header.Get("x-two"))
w.Header().Add("content-type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte(msg))
_, err := w.Write([]byte(msg))
if err != nil {
log.Fatal(err)
}
}

ts := httptest.NewServer(Compose(one, two, recover.Recover).Root(http.HandlerFunc(fn)))
Expand Down
4 changes: 2 additions & 2 deletions internal/middleware/recover/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func Recover(next http.Handler) http.Handler {

w.Header().Set(headers.ContentType, mimetypes.TextPlain)
w.WriteHeader(http.StatusTeapot)
w.Write([]byte(fmt.Sprintf("%s - an unexpected error has occurred", http.StatusText(http.StatusTeapot))))
w.Write([]byte(fmt.Sprintf("%v", recovery)))
_, _ = w.Write([]byte(fmt.Sprintf("%s - an unexpected error has occurred", http.StatusText(http.StatusTeapot))))
_, _ = w.Write([]byte(fmt.Sprintf("%v", recovery)))
}
}()

Expand Down
7 changes: 5 additions & 2 deletions reply/proxied_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ func TestForward(t *testing.T) {
assert.Equal(t, "proxied", r.Header.Get("x-proxy"))

w.WriteHeader(http.StatusCreated)
w.Write([]byte("hello world"))
_, err := w.Write([]byte("hello world"))
if err != nil {
t.Fatal(err)
}
}))

defer dest.Close()
Expand Down Expand Up @@ -61,7 +64,7 @@ func TestForward(t *testing.T) {
t.Fatal(err)
}

w.Write(b)
_, _ = w.Write(b)
}))

defer dest.Close()
Expand Down
5 changes: 4 additions & 1 deletion test/proxied_responses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func TestForward(t *testing.T) {
}

w.WriteHeader(http.StatusOK)
w.Write(b)
_, err = w.Write(b)
if err != nil {
t.Fatal(err)
}
}))

defer dest.Close()
Expand Down