|
1 | 1 | package flickr |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "net/url" |
| 10 | + "path" |
| 11 | + "reflect" |
| 12 | + "strings" |
4 | 13 | "testing" |
5 | 14 | ) |
6 | 15 |
|
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)) |
12 | 19 | } |
13 | 20 | } |
| 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 | +} |
0 commit comments