func TestEntryCreatedIfReadCloserIsNotNilOnError(t *testing.T) { c := ioutil.NopCloser(bytes.NewReader([]byte("uh-oh"))) sourceFn := tSourceFunc(c) out := new(bytes.Buffer) zip := NewZip(sourceFn) zip.Add("good", "error", "andgoodagain") _, err := zip.WriteTo(out) if err := zip.Close(); err != nil { t.Fatal(err) } assert.NotNil(t, err) assert.Equal(t, fmt.Sprintf("1 error(s):\n\n%s", "* uh-oh"), err.Error()) ze := err.(Error) assert.Equal(t, 1, len(err.(Error))) assert.Equal(t, "uh-oh", ze[0].Error()) gozipst.VerifyZip(t, out.Bytes(), []gozipst.Entries{ {"good", "Good!"}, {"error", "uh-oh"}, {"andgoodagain", "Good!"}, }) }
func TestZipFromHTTPSources(t *testing.T) { ts := tServer() defer ts.Close() url1 := fmt.Sprintf("%s/index.html", ts.URL) url2 := fmt.Sprintf("%s/posts", ts.URL) url3 := fmt.Sprintf("%s/api/data.json", ts.URL) out := new(bytes.Buffer) zip := NewZip(sources.HTTP) zip.Add(url1) zip.Add(url2, url3) n, err := zip.WriteTo(out) if err := zip.Close(); err != nil { t.Fatal(err) } assert.Nil(t, err) assert.Equal(t, int64(38), n) assert.Equal(t, int64(38), zip.N) assert.Equal(t, int64(38), zip.UncompressedSize) assert.Equal(t, int64(56), zip.CompressedSize) gozipst.VerifyZip(t, out.Bytes(), []gozipst.Entries{ {"index.html", "Hello World!"}, {"posts", "Post Body"}, {"data.json", `{"data": ["one"]}`}, }) }
func TestRateLimitError(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/users\/self\/feed\?access_token=\w+$`) mock.Expect("GET", reg).Respond(429, `{"meta": {"code": 429}}`) data, err := api.Users.SelfFeed() assert.Equal(t, data.Meta.Code, 429) assert.Equal(t, err.Error(), "The maximum number of requests per hour has been exceeded.") assert.TypeOf(t, "instagram.RateLimitError", err) }
func TestRateLimit(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/users\/self\/feed\?access_token=\w+$`) resp, _ := mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 200}}`) resp.Header.Add("X-Ratelimit-Remaining", "3000") resp.Header.Add("X-Ratelimit-Limit", "5000") api.Users.SelfFeed() assert.Equal(t, api.RateLimit.Remaining, int64(3000)) assert.Equal(t, api.RateLimit.Limit, int64(5000)) }
func TestRealTimeUnsubscribe(t *testing.T) { api, mock := tNewRealTime(t, "my-clientid", "a-secret") reg := regmc(`v1\/subscriptions\?client_id=[a-z-]+&client_secret=[a-z-]+&id=\d+$`) mock.Expect("DELETE", reg).Respond(200, `{"meta": {"code": 201}}`) data, _ := api.Unsubscribe("id", "1234") assert.Equal(t, data.Meta.Code, 201) }
func TestTagsName(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/tags\/\w+\?access_token=\w+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 200}}`) data, _ := api.Tags.Name("Foo") assert.Equal(t, data.Meta.Code, 200) assert.TypeOf(t, "*jsons.Tag", data) assert.TypeOf(t, "jsons.TagData", data.Data) }
func TestRealTimeSubscriptions(t *testing.T) { api, mock := tNewRealTime(t, "my-clientid", "a-secret") reg := regmc(`v1\/subscriptions\?client_id=[a-z-]+&client_secret=[a-z-]+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 202}}`) data, _ := api.Subscriptions() assert.Equal(t, data.Meta.Code, 202) assert.TypeOf(t, "*jsons.Subscriptions", data) assert.TypeOf(t, "[]jsons.SubscriptionData", data.Data) }
func TestCommentsComments(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/media\/\d+\/comments\?access_token=\w+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 200}}`) data, _ := api.Comments.Comments(12345) assert.Equal(t, data.Meta.Code, 200) assert.TypeOf(t, "*jsons.Comments", data) assert.TypeOf(t, "[]jsons.CommentData", data.Data) }
func TestUsersUser(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/users\/\d+\?access_token=`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 201}}`) data, _ := api.Users.User(1234) assert.Equal(t, data.Meta.Code, 201) assert.TypeOf(t, "*jsons.User", data) assert.TypeOf(t, "jsons.UserData", data.Data) }
func TestRelationshipsFollows(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/users\/\d+\/follows\?access_token=\w+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 200}}`) data, _ := api.Relationships.Follows(123) assert.Equal(t, data.Meta.Code, 200) assert.TypeOf(t, "*jsons.Users", data) assert.TypeOf(t, "[]jsons.UserData", data.Data) }
func TestRelationshipsRelationship(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/users\/\d+\/relationship\?access_token=\w+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 203}}`) data, _ := api.Relationships.Relationship(12345) assert.Equal(t, data.Meta.Code, 203) assert.TypeOf(t, "*jsons.Relationship", data) assert.TypeOf(t, "jsons.RelationshipData", data.Data) }
func TestRelationshipsSelfRequestedBy(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/users\/self\/requested-by\?access_token=\w+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 202}}`) data, _ := api.Relationships.SelfRequestedBy() assert.Equal(t, data.Meta.Code, 202) assert.TypeOf(t, "*jsons.Users", data) assert.TypeOf(t, "[]jsons.UserData", data.Data) }
func TestLikesPost(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/media\/\d+\/likes\?access_token=\w+$`) mock.Expect("POST", reg).Respond(200, `{"meta": {"code": 201}}`) data, _ := api.Likes.Post(12345) assert.Equal(t, data.Meta.Code, 201) assert.TypeOf(t, "*jsons.Like", data) assert.TypeOf(t, "jsons.LikeData", data.Data) }
func TestCommentsDelete(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/media\/\d+\/comments\/\d+\?access_token=\w+$`) mock.Expect("DELETE", reg).Respond(200, `{"meta": {"code": 202}}`) data, _ := api.Comments.Delete(12345, 67890) assert.Equal(t, data.Meta.Code, 202) assert.TypeOf(t, "*jsons.Comment", data) assert.TypeOf(t, "jsons.CommentData", data.Data) }
func TestLocationsLocation(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/locations\/\d+\?access_token=\w+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 200}}`) data, _ := api.Locations.Location(12345) assert.Equal(t, data.Meta.Code, 200) assert.TypeOf(t, "*jsons.Location", data) assert.TypeOf(t, "jsons.LocationData", data.Data) }
func TestMediaMedia(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/media\/\d+\?access_token=\w+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 200}}`) data, _ := api.Media.Media(1234) assert.Equal(t, 200, data.Meta.Code) assert.TypeOf(t, "*jsons.Media", data) assert.TypeOf(t, "jsons.MediaData", data.Data) }
func TestLocationsMediaRecent(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/locations\/\d+\/media\/recent\?access_token=\w+&min_id=\d+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 201}}`) data, _ := api.Locations.MediaRecent(12345, map[string]string{ "min_id": "67890", }) assert.Equal(t, data.Meta.Code, 201) assert.TypeOf(t, "*jsons.Locations", data) assert.TypeOf(t, "[]jsons.LocationData", data.Data) }
func TestZipFromFSSources(t *testing.T) { out := new(bytes.Buffer) zip := NewZip(sources.FS) zip.Add("sample/file1.txt") zip.Add("sample/file2.txt") zip.Add("sample/file3.txt") n, err := zip.WriteTo(out) if err := zip.Close(); err != nil { t.Fatal(err) } assert.Nil(t, err) assert.Equal(t, int64(11), n) assert.Equal(t, int64(11), zip.N) assert.Equal(t, int64(11), zip.UncompressedSize) assert.Equal(t, int64(29), zip.CompressedSize) gozipst.VerifyZip(t, out.Bytes(), []gozipst.Entries{ {"file1.txt", "One"}, {"file2.txt", "Two"}, {"file3.txt", "Three"}, }) }
func TestLocationsSearch(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/locations\/search\?access_token=\w+&distance=\d+&lat=(\d+\.\d+)&lng=(\d+\.\d+)$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 202}}`) data, _ := api.Locations.Search(40.7127, 74.0059, map[string]string{ "distance": "5000", }) assert.Equal(t, data.Meta.Code, 202) assert.TypeOf(t, "*jsons.Locations", data) assert.TypeOf(t, "[]jsons.LocationData", data.Data) }
func TestTagsMediaRecent(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/tags\/\w+\/media\/recent\?access_token=\w+&count=\d+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 201}}`) data, _ := api.Tags.MediaRecent("Foo", map[string]string{ "count": "50", }) assert.Equal(t, data.Meta.Code, 201) assert.TypeOf(t, "*jsons.Medias", data) assert.TypeOf(t, "[]jsons.MediaData", data.Data) }
func TestRelationshipsPost(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/users\/\d+\/relationship\?access_token=\w+$`) _, mres := mock.Expect("POST", reg).Respond(200, `{"meta": {"code": 204}}`) data, _ := api.Relationships.Post(12345, "follow") assert.Equal(t, data.Meta.Code, 204) assert.TypeOf(t, "*jsons.Relationship", data) assert.TypeOf(t, "jsons.RelationshipData", data.Data) mres.Body().Equals("action=follow") }
func TestCommentsPost(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/media\/\d+\/comments\?access_token=\w+$`) _, mres := mock.Expect("POST", reg).Respond(200, `{"meta": {"code": 201}}`) data, _ := api.Comments.Post(12345, "Hello World") assert.Equal(t, data.Meta.Code, 201) assert.TypeOf(t, "*jsons.Comment", data) assert.TypeOf(t, "jsons.CommentData", data.Data) mres.Body().Equals("text=Hello World") }
func TestUsersSearch(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/users\/search\?access_token=\w+&count=\d+&q=foo\+bar$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 205}}`) data, _ := api.Users.Search("foo bar", map[string]string{ "count": "50", }) assert.Equal(t, data.Meta.Code, 205) assert.TypeOf(t, "*jsons.Users", data) assert.TypeOf(t, "[]jsons.UserData", data.Data) }
func TestUsersSelfMediaLiked(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/users\/self\/media\/liked\?access_token=\w+&count=\d+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 204}}`) data, _ := api.Users.SelfMediaLiked(map[string]string{ "count": "50", }) assert.Equal(t, data.Meta.Code, 204) assert.TypeOf(t, "*jsons.Medias", data) assert.TypeOf(t, "[]jsons.MediaData", data.Data) }
func TestUsersMediaRecentCID(t *testing.T) { api, mock := tNewInstagram(t) reg := regmc(`v1\/users\/\d+\/media\/recent\?client_id=clientid&count=\d+&min_id=\d+$`) mock.Expect("GET", reg).Respond(200, `{"meta": {"code": 203}}`) data, _ := api.Users.MediaRecentCID(1234, "clientid", map[string]string{ "count": "50", "min_id": "12345678", }) assert.Equal(t, data.Meta.Code, 203) assert.TypeOf(t, "*jsons.Medias", data) assert.TypeOf(t, "[]jsons.MediaData", data.Data) }
func TestEndpointUrlWithParams(t *testing.T) { table := map[int]string{ UsersUser: "******", UsersMediaRecent: "/v1/users/12345/media/recent", } for k, v := range table { ep := NewEndpoint(k, query, 12345) assert.Equal(t, ep.URL(), &url.URL{ Scheme: "https", Host: "api.instagram.com", Path: v, RawQuery: "access_token=abcdefg", }) } }
func TestEndpointUrl(t *testing.T) { table := map[int]string{ UsersSelfFeed: "/v1/users/self/feed", UsersSelfMediaLiked: "/v1/users/self/media/liked", } for k, v := range table { ep := NewEndpoint(k, query) assert.Equal(t, ep.URL(), &url.URL{ Scheme: "https", Host: "api.instagram.com", Path: v, RawQuery: "access_token=abcdefg", }) } }
func TestAllowMethods(t *testing.T) { m := Allow("get", "POST") for _, v := range []struct { method string b bool }{ {"GET", true}, {"POST", true}, {"DELETE", false}, } { b := m.Allowed(&http.Request{ Method: v.method, }) assert.Equal(t, b, v.b) } }
func TestEndpointWithQuery(t *testing.T) { table := map[int]string{ UsersSearch: "/v1/users/search", } query.Add("q", "foo bar") for k, v := range table { ep := NewEndpoint(k, query) assert.Equal(t, ep.URL(), &url.URL{ Scheme: "https", Host: "api.instagram.com", Path: v, RawQuery: "access_token=abcdefg&q=foo+bar", }) } }
func TestAddEntry(t *testing.T) { sourceFn := tSourceFunc(nil) out := new(bytes.Buffer) zip := NewZip(sourceFn) zip.Add("good", "error", "andgoodagain") n, err := zip.WriteTo(out) r := bytes.NewBufferString(err.Error()) z, er := zip.AddEntry("archivr-errors.txt", r) if er != nil { t.Fatal(er) } if err := zip.Close(); err != nil { t.Fatal(err) } assert.NotNil(t, err) assert.Equal(t, fmt.Sprintf("1 error(s):\n\n%s", "* uh-oh"), err.Error()) assert.Equal(t, int64(10), n) assert.Equal(t, int64(20), z) assert.Equal(t, n+z, zip.N) assert.Equal(t, int64(30), zip.UncompressedSize) assert.Equal(t, int64(48), zip.CompressedSize) ze := err.(Error) assert.Equal(t, 1, len(err.(Error))) assert.Equal(t, "uh-oh", ze[0].Error()) gozipst.VerifyZip(t, out.Bytes(), []gozipst.Entries{ {"good", "Good!"}, {"andgoodagain", "Good!"}, {"archivr-errors.txt", "1 error(s):\n\n* uh-oh"}, }) }