-
Notifications
You must be signed in to change notification settings - Fork 22
/
roundtrip.go
115 lines (88 loc) · 1.97 KB
/
roundtrip.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package docs
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strings"
"github.com/pivotal-cf-experimental/warrant"
)
type RoundTrip struct {
Request *http.Request
Response *http.Response
}
func (r RoundTrip) Method() string {
return r.Request.Method
}
func (r RoundTrip) Path() string {
return guidRegexp.ReplaceAllLiteralString(r.Request.URL.Path, "{id}")
}
func (r RoundTrip) RequiredScopes() string {
authHeader := r.Request.Header.Get("Authorization")
if authHeader == "" {
return ""
}
parts := strings.Split(authHeader, " ")
if len(parts) != 2 {
return ""
}
tokensService := warrant.NewTokensService(warrant.Config{})
token, err := tokensService.Decode(parts[1])
if err != nil {
return ""
}
return strings.Join(token.Scopes, " ")
}
func (r RoundTrip) RequestHeaders() []string {
var headers []string
for key, values := range r.Request.Header {
for _, value := range values {
headers = append(headers, fmt.Sprintf("%s: %s", key, value))
}
}
sort.Strings(headers)
return headers
}
func (r RoundTrip) RequestBody() string {
if r.Request.Body == nil {
return ""
}
var input map[string]interface{}
err := json.NewDecoder(r.Request.Body).Decode(&input)
if err != nil {
return ""
}
output, err := json.MarshalIndent(input, "", " ")
if err != nil {
return ""
}
return string(output)
}
func (r RoundTrip) ResponseStatus() string {
return r.Response.Status
}
func (r RoundTrip) ResponseHeaders() []string {
var headers []string
for key, values := range r.Response.Header {
for _, value := range values {
headers = append(headers, fmt.Sprintf("%s: %s", key, value))
}
}
sort.Strings(headers)
return headers
}
func (r RoundTrip) ResponseBody() string {
if r.Response.Body == nil {
return ""
}
var input map[string]interface{}
err := json.NewDecoder(r.Response.Body).Decode(&input)
if err != nil {
return ""
}
output, err := json.MarshalIndent(input, "", " ")
if err != nil {
return ""
}
return string(output)
}