Skip to content

Commit

Permalink
feat: late set response data/body
Browse files Browse the repository at this point in the history
  • Loading branch information
vinyguedess committed Mar 10, 2022
1 parent 8cbe521 commit 60d744b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
1 change: 1 addition & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ func Handler(handler EasyHandler) MuxHandler {
for key, value := range receivedResponse.GetHeaders() {
responseWriter.Header().Set(key, value)
}
responseWriter.Write([]byte(receivedResponse.GetData()))
}
}
23 changes: 11 additions & 12 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Response struct {
headers map[string]string
responseWriter http.ResponseWriter
status int
data string
}

func NewResponse(responseWriter http.ResponseWriter) Response {
Expand Down Expand Up @@ -60,25 +61,23 @@ func (response *Response) GetStatus() int {
return response.status
}

func (response *Response) Json(data interface{}) *Response {
response.SetHeader("Content-Type", "application/json")
func (response *Response) GetData() string {
return response.data
}

func (response *Response) Json(data interface{}) *Response {
responseData, _ := json.Marshal(data)
response.responseWriter.Write(responseData)
response.data = string(responseData)

return response
return response.SetHeader("Content-Type", "application/json")
}

func (response *Response) Html(data string) *Response {
response.SetHeader("Content-Type", "text/html")
response.responseWriter.Write([]byte(data))

return response
response.data = data
return response.SetHeader("Content-Type", "text/html")
}

func (response *Response) Text(data string) *Response {
response.SetHeader("Content-Type", "plain/text")
response.responseWriter.Write([]byte(data))

return response
response.data = data
return response.SetHeader("Content-Type", "plain/text")
}
6 changes: 3 additions & 3 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestResponse_Json(t *testing.T) {
"hello": "world",
})

assert.Equal(t, "{\"hello\":\"world\"}", responseWriter.Body.String())
assert.Equal(t, "{\"hello\":\"world\"}", response.GetData())
assert.Equal(t, "application/json", response.GetHeader("Content-Type"))
}

Expand All @@ -54,7 +54,7 @@ func TestResponse_Html(t *testing.T) {
response.SetResponseWriter(responseWriter).
Html("<h1>Hello</h1>")

assert.Equal(t, "<h1>Hello</h1>", responseWriter.Body.String())
assert.Equal(t, "<h1>Hello</h1>", response.GetData())
assert.Equal(t, "text/html", response.GetHeader("Content-Type"))
}

Expand All @@ -65,6 +65,6 @@ func TestResponse_Text(t *testing.T) {
response.SetResponseWriter(responseWriter).
Text("oi ne")

assert.Equal(t, "oi ne", responseWriter.Body.String())
assert.Equal(t, "oi ne", response.GetData())
assert.Equal(t, "plain/text", response.GetHeader("Content-Type"))
}

0 comments on commit 60d744b

Please sign in to comment.