-
Notifications
You must be signed in to change notification settings - Fork 9
/
fakehttp.go
98 lines (87 loc) · 3.01 KB
/
fakehttp.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
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package fakehttp provides a fake HTTP server for tests that have dependencies using HTTP clients.
package fakehttp
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
glog "github.com/golang/glog" /* copybara-comment */
"google.golang.org/grpc/codes" /* copybara-comment */
"google.golang.org/grpc/status" /* copybara-comment */
"github.com/GoogleCloudPlatform/healthcare-federated-access-services/lib/httputils" /* copybara-comment: httputils */
)
// Handler handles HTTP requests.
type Handler func(req *http.Request) (body interface{}, err error)
// DefaultHandler is a default HTTP request handler that returns a nil body.
func DefaultHandler(req *http.Request) (interface{}, error) {
return nil, nil
}
// HTTP provides an HTTP server routing calls to its handler and a client connected to it.
type HTTP struct {
// Handler is handler for HTTP requests.
Handler Handler
// Server is the HTTP server. Uses handler for handling requests.
Server *httptest.Server
// Client is the HTTP client connected to the HTTP server.
Client *http.Client
}
// New creates a new HTTP server and client.
func New() (*HTTP, func() error) {
f := &HTTP{}
f.Handler = DefaultHandler
h := func(w http.ResponseWriter, req *http.Request) {
glog.Infof("HTTP Request: %+v", req)
defer glog.Infof("HTTP Response: %+v", req.Response)
resp, err := f.Handler(req)
if err != nil {
httputils.WriteError(w, err)
}
httputils.WriteNonProtoResp(w, resp)
}
f.Server = httptest.NewServer(http.HandlerFunc(h))
f.Client = f.Server.Client()
cleanup := func() error {
f.Server.Close()
return nil
}
return f, cleanup
}
// PrefixHandlers demuxes calls based on URL path prefix.
// The first handler whose prefix matches will be called.
type PrefixHandlers map[string]Handler
// Handler handles HTTP requests.
func (p PrefixHandlers) Handler(req *http.Request) (body interface{}, err error) {
path := req.URL.Path
for prefix, h := range p {
if strings.HasPrefix(path, prefix) {
return h(req)
}
}
return nil, status.Errorf(codes.NotFound, "no handler found for %q", path)
}
// DecodeResponse decodes the HTTP Response.Body JSON.
func DecodeResponse(resp *http.Response, v interface{}) error {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("ioutil.ReadAll(_) failed: %v", err)
}
if err := json.Unmarshal(b, v); err != nil {
return fmt.Errorf("json.Unmarshal(%q,%T) failed: %v", b, v, err)
}
return nil
}