-
Notifications
You must be signed in to change notification settings - Fork 67
/
server.go
87 lines (71 loc) · 2.04 KB
/
server.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
package mockingjay
import (
"encoding/json"
"log"
"net/http"
"reflect"
)
// Server allows you to configure a request and a corresponding response. It implements http.ServeHTTP so you can bind it to a port.
type Server struct {
endpoints []FakeEndpoint
requests []Request
}
// NewServer creates a new Server instance
func NewServer(endpoints []FakeEndpoint) *Server {
s := new(Server)
s.endpoints = endpoints
s.requests = make([]Request, 0)
return s
}
const requestsURL = "/requests"
const endpointsURL = "/mj-endpoints"
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.String() {
case endpointsURL:
s.serveEndpoints(w)
case requestsURL:
s.listAvailableRequests(w)
default:
mjRequest := NewRequest(r)
s.requests = append(s.requests, mjRequest)
cannedResponse := s.getResponse(mjRequest)
for name, value := range cannedResponse.Headers {
w.Header().Set(name, value)
}
w.WriteHeader(cannedResponse.Code)
w.Write([]byte(cannedResponse.Body))
}
}
func (s *Server) listAvailableRequests(w http.ResponseWriter) {
payload, err := json.Marshal(s.requests)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
w.Write(payload)
}
}
func (s *Server) getResponse(r Request) *response {
for _, endpoint := range s.endpoints {
if requestMatches(endpoint.Request, r) {
return &endpoint.Response
}
}
return newNotFound(r.Method, r.URI, r.Body, r.Headers, s.endpoints)
}
func requestMatches(a Request, b Request) bool {
headersOk := !(a.Headers != nil && !reflect.DeepEqual(a.Headers, b.Headers))
bodyOk := a.Body == "*" || a.Body == b.Body
urlOk := a.URI == b.URI
methodOk := a.Method == b.Method
return bodyOk && urlOk && methodOk && headersOk
}
func (s *Server) serveEndpoints(w http.ResponseWriter) {
endpointsBody, err := json.Marshal(s.endpoints)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Write(endpointsBody)
w.Header().Set("Content-Type", "application/json")
}