1
// Copyright 2014 ALTOROS
2
// Licensed under the AGPLv3, see LICENSE file for details.
12
// Response represents HTTPS client response
13
type Response struct {
17
// VerifyJSON checks the response has specified code and carries application/json body
18
func (r Response) VerifyJSON(code int) error {
19
return r.Verify(code, "application/json")
22
// Verify checks the response has specified code and body with specified content type
23
func (r Response) Verify(code int, contentType string) error {
24
if err := r.VerifyCode(code); err != nil {
27
if err := r.VerifyContentType(contentType); err != nil {
33
// VerifyCode checks the response has specified code
34
func (r Response) VerifyCode(code int) error {
35
if r.StatusCode != code {
36
return fmt.Errorf("expected HTTP code: %d, got code: %d, %s", code, r.StatusCode, r.Status)
41
// VerifyContentType checks the response has specified content type
42
func (r Response) VerifyContentType(contentType string) error {
43
if contentType == "" {
47
contentType = strings.ToLower(contentType)
49
vv, ok := r.Header["Content-Type"]
51
return fmt.Errorf("header Content-Type not found in response, expected \"%s\"", contentType)
54
for _, v := range vv {
55
v = strings.ToLower(v)
56
if strings.Contains(v, contentType) {
61
return fmt.Errorf("expected Content-Type: \"%s\", received \"%v\"", contentType, vv)