Creates goproxy.ProxyHttpServer and configures it to be used as a webserver for Hoverfly goproxy is given a non proxy handler that uses the Hoverfly request processing
(hoverfly *Hoverfly)
| 131 | // Creates goproxy.ProxyHttpServer and configures it to be used as a webserver for Hoverfly |
| 132 | // goproxy is given a non proxy handler that uses the Hoverfly request processing |
| 133 | func NewWebserverProxy(hoverfly *Hoverfly) *goproxy.ProxyHttpServer { |
| 134 | // creating proxy |
| 135 | proxy := goproxy.NewProxyHttpServer() |
| 136 | proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 137 | startTime := time.Now() |
| 138 | r.URL.Scheme = "http" |
| 139 | resp, journalIDChannel := hoverfly.processRequest(r) |
| 140 | id, _ := hoverfly.Journal.NewEntry(r, resp, hoverfly.Cfg.Mode, startTime) |
| 141 | sendJournalIDToPostServeAction(journalIDChannel, id) |
| 142 | body, err := util.GetResponseBody(resp) |
| 143 | |
| 144 | if err != nil { |
| 145 | log.Error("Error reading response body") |
| 146 | w.WriteHeader(500) |
| 147 | return |
| 148 | } |
| 149 | |
| 150 | for name, values := range resp.Header { |
| 151 | name = strings.ToLower(name) |
| 152 | |
| 153 | for _, value := range values { |
| 154 | w.Header().Add(name, value) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | w.WriteHeader(resp.StatusCode) |
| 159 | w.Write([]byte(body)) |
| 160 | |
| 161 | hoverfly.Counter.Count(hoverfly.Cfg.GetMode()) |
| 162 | }) |
| 163 | |
| 164 | if hoverfly.Cfg.Verbose { |
| 165 | proxy.OnRequest().DoFunc( |
| 166 | func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { |
| 167 | log.WithFields(log.Fields{ |
| 168 | "destination": r.Host, |
| 169 | "path": r.URL.Path, |
| 170 | "query": r.URL.RawQuery, |
| 171 | "method": r.Method, |
| 172 | "mode": hoverfly.Cfg.GetMode(), |
| 173 | }).Debug("got request..") |
| 174 | return r, nil |
| 175 | }) |
| 176 | } |
| 177 | |
| 178 | log.WithFields(log.Fields{ |
| 179 | "Destination": hoverfly.Cfg.Destination, |
| 180 | "WebserverPort": hoverfly.Cfg.ProxyPort, |
| 181 | "Mode": hoverfly.Cfg.GetMode(), |
| 182 | }).Info("Webserver prepared...") |
| 183 | |
| 184 | return proxy |
| 185 | } |
| 186 | |
| 187 | func unauthorizedError(request *http.Request, realm, message string) *http.Response { |
| 188 | response := auth.BasicUnauthorized(request, realm) |
no test coverage detected