diff --git a/testutils.go b/testutils.go deleted file mode 100644 index 5e5c94b..0000000 --- a/testutils.go +++ /dev/null @@ -1,109 +0,0 @@ -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{}) -} diff --git a/testutils_test.go b/testutils_test.go index 113e354..5e5c94b 100644 --- a/testutils_test.go +++ b/testutils_test.go @@ -1,13 +1,109 @@ package flickr import ( + "bytes" + "fmt" + "io/ioutil" + "net/http" + "net/http/httptest" + "net/url" + "path" + "reflect" + "strings" "testing" ) -func TestExpect(t *testing.T) { - t2 := testing.T{} - Expect(&t2, 1, 2) - if !t2.Failed() { - t.Errorf("Expect should fail") +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{}) +} diff --git a/testutils_test_test.go b/testutils_test_test.go new file mode 100644 index 0000000..113e354 --- /dev/null +++ b/testutils_test_test.go @@ -0,0 +1,13 @@ +package flickr + +import ( + "testing" +) + +func TestExpect(t *testing.T) { + t2 := testing.T{} + Expect(&t2, 1, 2) + if !t2.Failed() { + t.Errorf("Expect should fail") + } +}