-
Notifications
You must be signed in to change notification settings - Fork 25
/
testutils.go
109 lines (92 loc) · 2.67 KB
/
testutils.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
package flickr
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"path"
"reflect"
"strings"
"testing"
)
func Expect(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}
// testing keys were published at http://www.wackylabs.net/2011/12/oauth-and-flickr-part-2/
func GetTestClient() *FlickrClient {
args := url.Values{}
args.Set("oauth_nonce", "C2F26CD5C075BA9050AD8EE90644CF29")
args.Set("oauth_timestamp", "1316657628")
args.Set("oauth_consumer_key", "768fe946d252b119746fda82e1599980")
args.Set("oauth_signature_method", "HMAC-SHA1")
args.Set("oauth_version", "1.0")
args.Set("oauth_callback", "http://www.wackylabs.net/oauth/test")
return &FlickrClient{
EndpointUrl: "http://www.flickr.com/services/oauth/request_token",
HTTPClient: &http.Client{},
HTTPVerb: "GET",
Args: args,
ApiSecret: "1a3c208e172d3edc",
}
}
// mock the Flickr API
type RewriteTransport struct {
Transport http.RoundTripper
URL *url.URL
}
func (t RewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.URL.Scheme = t.URL.Scheme
req.URL.Host = t.URL.Host
req.URL.Path = path.Join(t.URL.Path, req.URL.Path)
rt := t.Transport
if rt == nil {
rt = http.DefaultTransport
}
return rt.RoundTrip(req)
}
func FlickrMock(code int, body string, contentType string) (*httptest.Server, *http.Client) {
if contentType == "" {
contentType = "text/plain;charset=UTF-8"
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
w.Header().Set("content-type", contentType)
fmt.Fprintln(w, body)
}))
u, _ := url.Parse(server.URL)
return server, &http.Client{Transport: RewriteTransport{URL: u}}
}
// A ReaderCloser to fake http.Response Body field
type FakeBody struct {
content *bytes.Buffer
}
func (f *FakeBody) Read(p []byte) (n int, err error) {
return f.content.Read(p)
}
func (f FakeBody) Close() error {
// noop
return nil
}
func NewFakeBody(s string) *FakeBody {
return &FakeBody{content: bytes.NewBufferString(s)}
}
// TODO
func AssertParamsInBody(t *testing.T, client *FlickrClient, params []string) {
var handler = func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
bodystr := string(body)
fmt.Fprintln(w, "Hello, client")
for _, p := range params {
needle := fmt.Sprintf(`Content-Disposition: form-data; name="%s"`, p)
Expect(t, strings.Contains(bodystr, needle), true)
}
}
ts := httptest.NewServer(http.HandlerFunc(handler))
defer ts.Close()
client.EndpointUrl = ts.URL
DoPost(client, &BasicResponse{})
}