-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstub_handler.go
More file actions
119 lines (100 loc) · 2.56 KB
/
stub_handler.go
File metadata and controls
119 lines (100 loc) · 2.56 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
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
116
117
118
119
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"sync"
)
type StubHandler struct {
stubs map[string]Stub
fallbackHandler http.Handler
stubMutex sync.Mutex
}
type Stub struct {
Method string `json:"method"`
Path string `json:"path"`
Body string `json:"body"`
Status int `json:"status"`
}
func (h *StubHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Always lock the stubs map for the duration of the request.
// Note that this effectively serializes all calls through StubHandler.
// As this is only used for testing this is deemed acceptable.
h.stubMutex.Lock()
defer h.stubMutex.Unlock()
if req.URL.Path == "/stubs" {
switch req.Method {
case "POST":
h.createStubHandler(w, req)
case "GET":
h.listStubsHandler(w, req)
case "DELETE":
h.deleteStubsHandler(w, req)
}
} else {
h.returnStubHandler(w, req)
}
}
func (h *StubHandler) load(r io.Reader) (err error) {
var stubs []Stub
jsonDecoder := json.NewDecoder(r)
err = jsonDecoder.Decode(&stubs)
if err != nil {
return
}
for _, stub := range stubs {
h.stubs[stub.Method+stub.Path] = stub
}
return
}
func (h *StubHandler) createStubHandler(w http.ResponseWriter, req *http.Request) {
var stub Stub
jsonDecoder := json.NewDecoder(req.Body)
err := jsonDecoder.Decode(&stub)
if err != nil {
log.Printf("Invalid JSON: %v", err)
w.WriteHeader(http.StatusBadRequest)
_, err = fmt.Fprintf(w, "Invalid JSON: %v", err)
if err != nil {
log.Printf("Could not send response to client due to: %v", err)
}
}
h.stubs[stub.Method+stub.Path] = stub
}
func (h *StubHandler) returnStubHandler(w http.ResponseWriter, req *http.Request) {
path := req.Method + req.URL.Path
query := req.URL.Query()
if len(query) > 0 {
path += "?" + query.Encode()
}
if stub, ok := h.stubs[path]; ok {
if stub.Status != 0 {
w.WriteHeader(stub.Status)
}
_, err := io.WriteString(w, stub.Body)
if err != nil {
log.Printf("Could not send response to client due to: %v", err)
}
} else {
h.fallbackHandler.ServeHTTP(w, req)
}
}
func (h *StubHandler) listStubsHandler(w http.ResponseWriter, req *http.Request) {
stubs := make([]Stub, 0, len(h.stubs))
encoder := json.NewEncoder(w)
for _, v := range h.stubs {
stubs = append(stubs, v)
}
encoder.Encode(stubs)
}
func (h *StubHandler) deleteStubsHandler(w http.ResponseWriter, req *http.Request) {
h.stubs = make(map[string]Stub)
}
func NewStubHandler() *StubHandler {
var s StubHandler
s.fallbackHandler = http.HandlerFunc(http.NotFound)
s.stubs = make(map[string]Stub)
return &s
}