Skip to content

Commit c3031f9

Browse files
committed
testutils.go renamed to testutils_test.go
testutils.go imports the testing package. It shouldn't be builded otherwise "flag" package shows all flags from the "testing" package
1 parent f97edeb commit c3031f9

File tree

3 files changed

+114
-114
lines changed

3 files changed

+114
-114
lines changed

testutils.go

Lines changed: 0 additions & 109 deletions
This file was deleted.

testutils_test.go

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,109 @@
11
package flickr
22

33
import (
4+
"bytes"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
"net/http/httptest"
9+
"net/url"
10+
"path"
11+
"reflect"
12+
"strings"
413
"testing"
514
)
615

7-
func TestExpect(t *testing.T) {
8-
t2 := testing.T{}
9-
Expect(&t2, 1, 2)
10-
if !t2.Failed() {
11-
t.Errorf("Expect should fail")
16+
func Expect(t *testing.T, a interface{}, b interface{}) {
17+
if a != b {
18+
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
1219
}
1320
}
21+
22+
// testing keys were published at http://www.wackylabs.net/2011/12/oauth-and-flickr-part-2/
23+
func GetTestClient() *FlickrClient {
24+
args := url.Values{}
25+
args.Set("oauth_nonce", "C2F26CD5C075BA9050AD8EE90644CF29")
26+
args.Set("oauth_timestamp", "1316657628")
27+
args.Set("oauth_consumer_key", "768fe946d252b119746fda82e1599980")
28+
args.Set("oauth_signature_method", "HMAC-SHA1")
29+
args.Set("oauth_version", "1.0")
30+
args.Set("oauth_callback", "http://www.wackylabs.net/oauth/test")
31+
32+
return &FlickrClient{
33+
EndpointUrl: "http://www.flickr.com/services/oauth/request_token",
34+
HTTPClient: &http.Client{},
35+
HTTPVerb: "GET",
36+
Args: args,
37+
ApiSecret: "1a3c208e172d3edc",
38+
}
39+
}
40+
41+
// mock the Flickr API
42+
type RewriteTransport struct {
43+
Transport http.RoundTripper
44+
URL *url.URL
45+
}
46+
47+
func (t RewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) {
48+
req.URL.Scheme = t.URL.Scheme
49+
req.URL.Host = t.URL.Host
50+
req.URL.Path = path.Join(t.URL.Path, req.URL.Path)
51+
rt := t.Transport
52+
if rt == nil {
53+
rt = http.DefaultTransport
54+
}
55+
return rt.RoundTrip(req)
56+
}
57+
58+
func FlickrMock(code int, body string, contentType string) (*httptest.Server, *http.Client) {
59+
if contentType == "" {
60+
contentType = "text/plain;charset=UTF-8"
61+
}
62+
63+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
64+
w.WriteHeader(code)
65+
w.Header().Set("content-type", contentType)
66+
fmt.Fprintln(w, body)
67+
}))
68+
69+
u, _ := url.Parse(server.URL)
70+
71+
return server, &http.Client{Transport: RewriteTransport{URL: u}}
72+
}
73+
74+
// A ReaderCloser to fake http.Response Body field
75+
type FakeBody struct {
76+
content *bytes.Buffer
77+
}
78+
79+
func (f *FakeBody) Read(p []byte) (n int, err error) {
80+
return f.content.Read(p)
81+
}
82+
83+
func (f FakeBody) Close() error {
84+
// noop
85+
return nil
86+
}
87+
88+
func NewFakeBody(s string) *FakeBody {
89+
return &FakeBody{content: bytes.NewBufferString(s)}
90+
}
91+
92+
// TODO
93+
func AssertParamsInBody(t *testing.T, client *FlickrClient, params []string) {
94+
var handler = func(w http.ResponseWriter, r *http.Request) {
95+
body, _ := ioutil.ReadAll(r.Body)
96+
bodystr := string(body)
97+
fmt.Fprintln(w, "Hello, client")
98+
for _, p := range params {
99+
needle := fmt.Sprintf(`Content-Disposition: form-data; name="%s"`, p)
100+
Expect(t, strings.Contains(bodystr, needle), true)
101+
}
102+
}
103+
104+
ts := httptest.NewServer(http.HandlerFunc(handler))
105+
defer ts.Close()
106+
107+
client.EndpointUrl = ts.URL
108+
DoPost(client, &BasicResponse{})
109+
}

testutils_test_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package flickr
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestExpect(t *testing.T) {
8+
t2 := testing.T{}
9+
Expect(&t2, 1, 2)
10+
if !t2.Failed() {
11+
t.Errorf("Expect should fail")
12+
}
13+
}

0 commit comments

Comments
 (0)