-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresponse.go
66 lines (56 loc) · 1.48 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package httpfake
import (
"encoding/json"
"fmt"
"net/http"
)
// Response stores the settings defined by the request handler
// of how it will respond the request back
type Response struct {
StatusCode int
BodyBuffer []byte
Header http.Header
}
// NewResponse creates a new Response
func NewResponse() *Response {
return &Response{
Header: make(http.Header),
}
}
// Status sets the response status
func (r *Response) Status(status int) *Response {
r.StatusCode = status
return r
}
// SetHeader sets the a HTTP header to the response
func (r *Response) SetHeader(key, value string) *Response {
r.Header.Set(key, value)
return r
}
// AddHeader adds a HTTP header into the response
func (r *Response) AddHeader(key, value string) *Response {
r.Header.Add(key, value)
return r
}
// Body sets the response body from a byte array
func (r *Response) Body(body []byte) *Response {
r.BodyBuffer = body
return r
}
// BodyString sets the response body from a string
// Example:
// BodyString(`[{"username": "dreamer"}]`)
func (r *Response) BodyString(body string) *Response {
return r.Body([]byte(body))
}
// BodyStruct sets the response body from a struct.
// The provided struct will be marsheled to json internally.
// Example:
// BodyStruct(&entity.User{UserName: "dreamer"})
func (r *Response) BodyStruct(body interface{}) *Response {
b, err := json.Marshal(body)
if err != nil {
printError(fmt.Sprintf("marshalling body %#v failed with %v", body, err))
}
return r.Body(b)
}