-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
59 lines (52 loc) · 1.57 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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package testing
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
apihttp "github.com/juju/juju/apiserver/http"
"github.com/juju/juju/apiserver/params"
)
// HTTPResponse is an HTTP response for use in testing.
type HTTPResponse struct {
http.Response
// Buffer is the file underlying Body.
Buffer bytes.Buffer
}
// NewHTTPResponse returns an HTTP response with an OK status,
// no headers set, and an empty body.
func NewHTTPResponse() *HTTPResponse {
resp := HTTPResponse{
Response: http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
},
}
resp.Body = ioutil.NopCloser(&resp.Buffer)
return &resp
}
// NewErrorResponse returns an HTTP response with the status and
// body set to the provided values.
func NewErrorResponse(statusCode int, msg string) *HTTPResponse {
resp := NewHTTPResponse()
resp.StatusCode = statusCode
if _, err := resp.Buffer.WriteString(msg); err != nil {
panic(fmt.Sprintf("could not write to buffer: %v", err))
}
return resp
}
// NewFailureResponse returns an HTTP response with the status set
// to 500 (Internal Server Error) and the body set to the JSON-encoded
// error.
func NewFailureResponse(failure *params.Error) *HTTPResponse {
resp := NewHTTPResponse()
resp.StatusCode = http.StatusInternalServerError
resp.Header.Set("Content-Type", apihttp.CTypeJSON)
if err := json.NewEncoder(&resp.Buffer).Encode(failure); err != nil {
panic(fmt.Sprintf("could not JSON-encode failure: %v", err))
}
return resp
}