forked from cockroachdb/cockroach
/
http.go
136 lines (124 loc) · 4.28 KB
/
http.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2015 The Cockroach Authors.
//
// 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.
//
// Author: Matt Tracy (matt@cockroachlabs.com)
package testutils
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/cockroachdb/cockroach/base"
"github.com/cockroachdb/cockroach/util"
"github.com/gogo/protobuf/proto"
)
// TestHTTPSession is a helper for tests which want to exercise repeated simple
// HTTP requests against the same server, especially where no errors are
// expected in the HTTP layer.
type TestHTTPSession struct {
t util.Tester
client *http.Client
baseURL string
}
// NewTestHTTPSession constructs a new TestHTTPSession. The session will
// instantiate a client using the based base context. All HTTP requests from the
// session will be sent to the given baseUrl.
//
// baseUrl should be specified *without* a request scheme (i.e. "http://"); the
// request scheme will be used from the context.
//
// If an error occurs in HTTP layer during any session operation, a Fatal method
// will be called on the supplied t.Tester.
func NewTestHTTPSession(t util.Tester, ctx *base.Context, baseURL string) *TestHTTPSession {
client, err := ctx.GetHTTPClient()
if err != nil {
t.Fatalf("error creating client: %s", err)
}
return &TestHTTPSession{
t: t,
client: client,
baseURL: ctx.HTTPRequestScheme() + "://" + baseURL,
}
}
// Post performs an http POST of the given bytes to the given relative URL. Any
// response returned from the request will be returned from this method as a
// byte slice.
func (ths *TestHTTPSession) Post(relative string, body []byte) []byte {
req, err := http.NewRequest("POST", ths.baseURL+relative, bytes.NewBuffer(body))
if err != nil {
ths.t.Fatal(err)
}
// All requests currently accept JSON.
req.Header.Set(util.ContentTypeHeader, util.JSONContentType)
req.Header.Set(util.AcceptHeader, util.JSONContentType)
resp, err := ths.client.Do(req)
if err != nil {
ths.t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
respBody, _ := ioutil.ReadAll(resp.Body)
ths.t.Fatalf("unexpected status code: %v, %s", resp.StatusCode, respBody)
}
returnedContentType := resp.Header.Get(util.ContentTypeHeader)
if returnedContentType != util.JSONContentType {
ths.t.Fatalf("unexpected content type: %v", returnedContentType)
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
ths.t.Fatal(err)
}
return respBody
}
// PostProto handles the common case where both the data posted and the expected
// response are encoded protobuffers. This method handles the marshalling of
// those buffers.
func (ths *TestHTTPSession) PostProto(relative string, msg proto.Message, out interface{}) {
requestBytes, err := json.Marshal(msg)
if err != nil {
ths.t.Fatal(err)
}
resp := ths.Post(relative, requestBytes)
if err := json.Unmarshal(resp, out); err != nil {
ths.t.Fatal(err)
}
}
// Get performs an http GET request to the given relative URL. Any response
// returned from the request will be returned from this method as a byte slice.
func (ths *TestHTTPSession) Get(relative string) []byte {
req, err := http.NewRequest("GET", ths.baseURL+relative, nil)
if err != nil {
ths.t.Fatal(err)
}
// All requests currently accept JSON.
req.Header.Set(util.AcceptHeader, util.JSONContentType)
resp, err := ths.client.Do(req)
if err != nil {
ths.t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
ths.t.Fatalf("unexpected status code: %v, %s", resp.StatusCode, body)
}
returnedContentType := resp.Header.Get(util.ContentTypeHeader)
if returnedContentType != util.JSONContentType {
ths.t.Fatalf("unexpected content type: %v", returnedContentType)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
ths.t.Fatal(err)
}
return body
}