Debian Codegraph @

photon/internal/mocks/httpserver.go

raw
1 // Copyright (c) 2016 VMware, Inc. All Rights Reserved.
2 //
3 // This product is licensed to you under the Apache License, Version 2.0 (the "License").
4 // You may not use this product except in compliance with the License.
5 //
6 // This product may include a number of subcomponents with separate copyright notices and
7 // license terms. Your use of these subcomponents is subject to the terms and conditions
8 // of the subcomponent's license, as noted in the LICENSE file.
9
10 package mocks
11
12 import (
13 "encoding/json"
14 "fmt"
15 "net/http"
16 "net/http/httptest"
17 "net/url"
18 "strconv"
19 "strings"
20 )
21
22 type ServerResponseData struct {
23 StatuCode *int
24 Body *string
25 }
26
27 type Server struct {
28 HttpServer *httptest.Server
29 DefaultResponse *ServerResponseData
30 Responses map[string]*ServerResponseData
31 }
32
33 func newUnstartedTestServer() (server *Server) {
34 status := 200
35 body := ""
36
37 server = &Server{
38 nil,
39 &ServerResponseData{&status, &body},
40 make(map[string]*ServerResponseData),
41 }
42
43 server.HttpServer = httptest.NewUnstartedServer(
44 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
45 w.Header().Set("Content-Type", "application/json")
46
47 var response *ServerResponseData
48 for k, v := range server.Responses {
49 if strings.HasPrefix(r.URL.Path, k) {
50 response = v
51 break
52 }
53 }
54
55 if response == nil {
56 response = server.DefaultResponse
57 }
58
59 w.WriteHeader(*response.StatuCode)
60 fmt.Fprintln(w, *response.Body)
61 }))
62 return
63 }
64
65 func NewTestServer() (server *Server) {
66 server = newUnstartedTestServer()
67 server.HttpServer.Start()
68 return
69 }
70
71 func NewTlsTestServer() (server *Server) {
72 server = newUnstartedTestServer()
73 server.HttpServer.StartTLS()
74 return
75 }
76
77 func (s *Server) Close() {
78 if s.HttpServer != nil {
79 s.HttpServer.Close()
80 }
81 }
82
83 func (s *Server) SetResponse(status int, body string) {
84 s.DefaultResponse = &ServerResponseData{StatuCode: &status, Body: &body}
85 }
86
87 func (s *Server) SetResponseJson(status int, v interface{}) {
88 s.SetResponse(status, s.toJson(v))
89 }
90
91 func (s *Server) SetResponseForPath(path string, status int, body string) {
92 s.Responses[path] = &ServerResponseData{&status, &body}
93 }
94
95 func (s *Server) SetResponseJsonForPath(path string, status int, v interface{}) {
96 s.SetResponseForPath(path, status, s.toJson(v))
97 }
98
99 func (s *Server) GetAddressAndPort() (address string, port int, err error) {
100 serverURL, err := url.Parse(s.HttpServer.URL)
101 if err != nil {
102 return
103 }
104
105 hostList := strings.Split(serverURL.Host, ":")
106 address = hostList[0]
107 port, err = strconv.Atoi(hostList[1])
108 if err != nil {
109 return
110 }
111
112 return
113 }
114
115 func (s *Server) toJson(v interface{}) string {
116 res, err := json.Marshal(v)
117 if err != nil {
118 // Since this method is only for testing, don't return
119 // any errors, just panic.
120 panic("Error serializing struct into JSON")
121 }
122 // json.Marshal returns []byte, convert to string
123 return string(res[:])
124 }