~juju-qa/ubuntu/yakkety/juju/juju-1.25.8

« back to all changes in this revision

Viewing changes to src/github.com/altoros/gosigma/https/response.go

  • Committer: Nicholas Skaggs
  • Date: 2016-12-02 17:28:37 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161202172837-jkrbdlyjcxtrii2n
Initial commit of 1.25.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 ALTOROS
 
2
// Licensed under the AGPLv3, see LICENSE file for details.
 
3
 
 
4
package https
 
5
 
 
6
import (
 
7
        "fmt"
 
8
        "net/http"
 
9
        "strings"
 
10
)
 
11
 
 
12
// Response represents HTTPS client response
 
13
type Response struct {
 
14
        *http.Response
 
15
}
 
16
 
 
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")
 
20
}
 
21
 
 
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 {
 
25
                return err
 
26
        }
 
27
        if err := r.VerifyContentType(contentType); err != nil {
 
28
                return err
 
29
        }
 
30
        return nil
 
31
}
 
32
 
 
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)
 
37
        }
 
38
        return nil
 
39
}
 
40
 
 
41
// VerifyContentType checks the response has specified content type
 
42
func (r Response) VerifyContentType(contentType string) error {
 
43
        if contentType == "" {
 
44
                return nil
 
45
        }
 
46
 
 
47
        contentType = strings.ToLower(contentType)
 
48
 
 
49
        vv, ok := r.Header["Content-Type"]
 
50
        if !ok {
 
51
                return fmt.Errorf("header Content-Type not found in response, expected \"%s\"", contentType)
 
52
        }
 
53
 
 
54
        for _, v := range vv {
 
55
                v = strings.ToLower(v)
 
56
                if strings.Contains(v, contentType) {
 
57
                        return nil
 
58
                }
 
59
        }
 
60
 
 
61
        return fmt.Errorf("expected Content-Type: \"%s\", received \"%v\"", contentType, vv)
 
62
}