-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse-parser.go
More file actions
50 lines (39 loc) · 1.24 KB
/
response-parser.go
File metadata and controls
50 lines (39 loc) · 1.24 KB
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
package arp
import (
"errors"
"fmt"
"net/http"
)
var (
InvalidContentType = errors.New("Invalid Content Type, falling back to binary")
)
type ResponseParser interface {
Parse(response *http.Response) (map[string]interface{}, interface{}, error)
}
type ResponseParserHandler map[string]ResponseParser
func (rh *ResponseParserHandler) Register(responseType string, handler ResponseParser) {
(*rh)[responseType] = handler
}
func (rh *ResponseParserHandler) LoadDefaults() {
(*rh) = make(map[string]ResponseParser)
rh.Register("json", &JSONParser{})
rh.Register("binary", &BinaryParser{})
}
func (rh *ResponseParserHandler) Handle(test *TestCase, response *http.Response) (map[string]interface{}, interface{}, error) {
responseType := test.Config.Response.Type
parser, exists := (*rh)[responseType]
if !exists {
return nil, nil, fmt.Errorf("No response parser defined for type \"%v\"", responseType)
}
js, raw, err := parser.Parse(response)
if err == InvalidContentType {
// binary parser should always be available as a fallback option for unsupported/unexpected
// data types
fallbackParser := BinaryParser{
Fallback: true,
SavePath: test.Config.Response.FilePath,
}
return fallbackParser.Parse(response)
}
return js, raw, err
}