Skip to content

Commit

Permalink
beatlabs#128 unexport response read writer
Browse files Browse the repository at this point in the history
Signed-off-by: Vangelis Katikaridis <[email protected]>
  • Loading branch information
drakos74 committed May 2, 2020
1 parent 7499a8b commit 5e6f3b9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
26 changes: 13 additions & 13 deletions component/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,51 +41,51 @@ func NewResponse(p interface{}) *Response {
// ProcessorFunc definition of a function type for processing sync requests.
type ProcessorFunc func(context.Context, *Request) (*Response, error)

// ResponseReadWriter is a response writer able to read the payload.
type ResponseReadWriter struct {
// responseReadWriter is a response writer able to read the payload.
type responseReadWriter struct {
buffer *bytes.Buffer
len int
header http.Header
statusCode int
}

// NewResponseReadWriter creates a new ResponseReadWriter.
func NewResponseReadWriter() *ResponseReadWriter {
return &ResponseReadWriter{
// newResponseReadWriter creates a new responseReadWriter.
func newResponseReadWriter() *responseReadWriter {
return &responseReadWriter{
buffer: new(bytes.Buffer),
header: make(http.Header),
}
}

// Read reads the ResponsereadWriter payload.
func (rw *ResponseReadWriter) Read(p []byte) (n int, err error) {
// read reads the responsereadWriter payload.
func (rw *responseReadWriter) read(p []byte) (n int, err error) {
return rw.buffer.Read(p)
}

// ReadAll returns the response payload bytes.
func (rw *ResponseReadWriter) ReadAll() ([]byte, error) {
// readAll returns the response payload bytes.
func (rw *responseReadWriter) readAll() ([]byte, error) {
if rw.len == 0 {
// nothing has been written
return []byte{}, nil
}
b := make([]byte, rw.len)
_, err := rw.Read(b)
_, err := rw.read(b)
return b, err
}

// Header returns the Header object.
func (rw *ResponseReadWriter) Header() http.Header {
func (rw *responseReadWriter) Header() http.Header {
return rw.header
}

// Write writes the provied bytes to the byte buffer.
func (rw *ResponseReadWriter) Write(p []byte) (int, error) {
func (rw *responseReadWriter) Write(p []byte) (int, error) {
rw.len = len(p)
return rw.buffer.Write(p)
}

// WriteHeader writes the header status code.
func (rw *ResponseReadWriter) WriteHeader(statusCode int) {
func (rw *responseReadWriter) WriteHeader(statusCode int) {
rw.statusCode = statusCode
}

Expand Down
16 changes: 8 additions & 8 deletions component/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,48 @@ func TestNewResponse(t *testing.T) {
}

func TestResponseReadWriter_Header(t *testing.T) {
rw := NewResponseReadWriter()
rw := newResponseReadWriter()
rw.Header().Set("key", "value")
assert.Equal(t, "value", rw.Header().Get("key"))
}

func TestResponseReadWriter_StatusCode(t *testing.T) {
rw := NewResponseReadWriter()
rw := newResponseReadWriter()
rw.WriteHeader(100)
assert.Equal(t, 100, rw.statusCode)
}

func TestResponseReadWriter_ReadWrite(t *testing.T) {
rw := NewResponseReadWriter()
rw := newResponseReadWriter()
str := "body"
i, err := rw.Write([]byte(str))
assert.NoError(t, err)

r := make([]byte, i)
j, err := rw.Read(r)
j, err := rw.read(r)
assert.NoError(t, err)

assert.Equal(t, i, j)
assert.Equal(t, str, string(r))
}

func TestResponseReadWriter_ReadWriteAll(t *testing.T) {
rw := NewResponseReadWriter()
rw := newResponseReadWriter()
str := "body"
i, err := rw.Write([]byte(str))
assert.NoError(t, err)

b, err := rw.ReadAll()
b, err := rw.readAll()
assert.NoError(t, err)

assert.Equal(t, i, len(b))
assert.Equal(t, str, string(b))
}

func TestResponseReadWriter_ReadAllEmpty(t *testing.T) {
rw := NewResponseReadWriter()
rw := newResponseReadWriter()

b, err := rw.ReadAll()
b, err := rw.readAll()
assert.NoError(t, err)

assert.Equal(t, 0, len(b))
Expand Down
6 changes: 3 additions & 3 deletions component/http/route_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func wrapHandlerFunc(handler http.HandlerFunc, rc *RouteCache) http.HandlerFunc
} else {
propagateHeaders(resp.header, response.Header())
if i, err := response.Write(resp.bytes); err != nil {
log.Errorf("could not write cache processor result into response %d: %v", i, err)
log.Errorf("could not Write cache processor result into response %d: %v", i, err)
}
}
}
Expand All @@ -60,9 +60,9 @@ func wrapHandlerFunc(handler http.HandlerFunc, rc *RouteCache) http.HandlerFunc
func handlerExecutor(_ http.ResponseWriter, request *http.Request, hnd http.HandlerFunc) executor {
return func(now int64, key string) *cachedResponse {
var err error
responseReadWriter := NewResponseReadWriter()
responseReadWriter := newResponseReadWriter()
hnd(responseReadWriter, request)
payload, err := responseReadWriter.ReadAll()
payload, err := responseReadWriter.readAll()
if err == nil {
return &cachedResponse{
response: &cacheHandlerResponse{
Expand Down
6 changes: 3 additions & 3 deletions component/http/route_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestHandlerWrapper(t *testing.T) {
type arg struct {
handler http.HandlerFunc
req *http.Request
rsp *ResponseReadWriter
rsp *responseReadWriter
}

args := []arg{
Expand All @@ -81,7 +81,7 @@ func TestHandlerWrapper(t *testing.T) {
assert.NoError(t, err)
assert.True(t, i > 0)
},
rsp: NewResponseReadWriter(),
rsp: newResponseReadWriter(),
req: &http.Request{RequestURI: "http://www.localhost.com"},
},
}
Expand All @@ -94,7 +94,7 @@ func TestHandlerWrapper(t *testing.T) {

wrappedHandler(testArg.rsp, testArg.req)

b, err := testArg.rsp.ReadAll()
b, err := testArg.rsp.readAll()
assert.NoError(t, err)
assert.NotNil(t, b)
assert.True(t, len(b) > 0)
Expand Down

0 comments on commit 5e6f3b9

Please sign in to comment.