func TestSearchBrowserLauncher(t *testing.T) { browser := searchBrowserLauncher("darwin") assert.Equal(t, "open", browser) browser = searchBrowserLauncher("windows") assert.Equal(t, "cmd /c start", browser) }
func TestResolveRequestQuery(t *testing.T) { setup := Setup(t) defer setup.Teardown() setup.Mux.HandleFunc("/q", func(w http.ResponseWriter, r *http.Request) { q := r.URL.Query() assert.Equal(t, "1", q.Get("a")) assert.Equal(t, "4", q.Get("b")) assert.Equal(t, "3", q.Get("c")) assert.Equal(t, "2", q.Get("d")) assert.Equal(t, "1", q.Get("e")) w.WriteHeader(123) w.Write([]byte("ok")) }) assert.Equal(t, "1", setup.Client.Query.Get("a")) assert.Equal(t, "1", setup.Client.Query.Get("b")) setup.Client.Query.Set("b", "2") setup.Client.Query.Set("c", "3") req, err := setup.Client.NewRequest("/q?d=4") assert.Equal(t, nil, err) req.Query.Set("b", "4") req.Query.Set("c", "3") req.Query.Set("d", "2") req.Query.Set("e", "1") res := req.Get() assert.Equal(t, nil, err) assert.Equal(t, 123, res.StatusCode) }
func TestUploadsService_UploadAsset(t *testing.T) { setup() defer tearDown() file, err := ioutil.TempFile("", "octokit-test-upload-") assert.Equal(t, nil, err) file.WriteString("this is a test") fi, err := file.Stat() assert.Equal(t, nil, err) file.Close() mux.HandleFunc("/repos/octokit/Hello-World/releases/123/assets", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") testHeader(t, r, "Content-Type", "text/plain") assert.Equal(t, fi.Size(), r.ContentLength) respondWithStatus(w, 201) }) link := Hyperlink("/repos/octokit/Hello-World/releases/123/assets{?name}") url, err := link.Expand(M{"name": fi.Name()}) assert.Equal(t, nil, err) open, _ := os.Open(file.Name()) result := client.Uploads(url).UploadAsset(open, "text/plain", fi.Size()) fmt.Println(result) assert.T(t, !result.HasError()) assert.Equal(t, 201, result.Response.StatusCode) }
func TestSuccessfulGetWithoutOutput(t *testing.T) { setup := Setup(t) defer setup.Teardown() setup.Mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "GET", r.Method) head := w.Header() head.Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write([]byte(`{"id": 1, "login": "******"}`)) }) client := setup.Client user := &TestUser{} req, err := client.NewRequest("user") assert.Equal(t, nil, err) res := req.Get() assert.Equal(t, false, res.IsError()) assert.Equal(t, false, res.IsApiError()) assert.Tf(t, !res.IsError(), "Response shouldn't have error") assert.Equal(t, 200, res.StatusCode) assert.Equal(t, false, res.BodyClosed) assert.Equal(t, 0, user.Id) assert.Equal(t, "", user.Login) dec := json.NewDecoder(res.Body) dec.Decode(user) assert.Equal(t, 1, user.Id) assert.Equal(t, "sawyer", user.Login) }
func TestGitTreesService_One_Recursive(t *testing.T) { setup() defer tearDown() mux.HandleFunc("/repos/pengwynn/flint/git/trees/master", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") respondWithJSON(w, loadFixture("tree_recursive.json")) }) url, err := GitTreesURL.Expand(M{ "owner": "pengwynn", "repo": "flint", "sha": "master", "recursive": "1", }) assert.Equal(t, nil, err) tree, result := client.GitTrees(url).One() assert.T(t, !result.HasError()) assert.Equal(t, "9c1337e761bbd517f3cc1b5acb9373b17f4810e8", tree.Sha) assert.Equal(t, "https://api.github.com/repos/pengwynn/flint/git/trees/9c1337e761bbd517f3cc1b5acb9373b17f4810e8", tree.URL) entries := tree.Tree assert.Equal(t, 15, len(entries)) }
func TestSuccessfulGet(t *testing.T) { setup := Setup(t) defer setup.Teardown() setup.Mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "GET", r.Method) head := w.Header() head.Set("Content-Type", "application/json") link := `<https://api.github.com/user/repos?page=3&per_page=100>; rel="next", <https://api.github.com/user/repos?page=50&per_page=100>; rel="last"` head.Set("Link", link) w.WriteHeader(http.StatusOK) w.Write([]byte(`{"id": 1, "login": "******"}`)) }) client := setup.Client user := &TestUser{} req, err := client.NewRequest("user") assert.Equal(t, nil, err) res := req.Get() assert.Equal(t, false, res.IsError()) assert.Equal(t, false, res.IsApiError()) assert.Equal(t, nil, res.Decode(user)) assert.Equal(t, 200, res.StatusCode) assert.Equal(t, 1, user.Id) assert.Equal(t, "sawyer", user.Login) mheader := res.MediaHeader assert.Equal(t, "https://api.github.com/user/repos?page=3&per_page=100", string(mheader.Relations["next"])) assert.Equal(t, "https://api.github.com/user/repos?page=50&per_page=100", string(mheader.Relations["last"])) }
func TestUsersService_All(t *testing.T) { setup() defer tearDown() mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") rr := regexp.MustCompile(`users\?since=\d+`) assert.Tf(t, rr.MatchString(r.URL.String()), "Regexp should match users?since=\\d+") header := w.Header() link := fmt.Sprintf(`<%s>; rel="next", <%s>; rel="first"`, testURLOf("users?since=135"), testURLOf("users{?since}")) header.Set("Link", link) respondWithJSON(w, loadFixture("users.json")) }) url, err := UserURL.Expand(M{"since": 1}) assert.Equal(t, nil, err) q := url.Query() q.Set("since", "1") url.RawQuery = q.Encode() allUsers, result := client.Users(url).All() assert.T(t, !result.HasError()) assert.Equal(t, 1, len(allUsers)) assert.Equal(t, testURLStringOf("users?since=135"), string(*result.NextPage)) nextPageURL, err := result.NextPage.Expand(nil) assert.Equal(t, nil, err) allUsers, result = client.Users(nextPageURL).All() assert.T(t, !result.HasError()) assert.Equal(t, 1, len(allUsers)) }
func TestRepositoresService_One(t *testing.T) { setup() defer tearDown() mux.HandleFunc("/repos/jingweno/octokat", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") respondWithJSON(w, loadFixture("repository.json")) }) url, err := RepositoryURL.Expand(M{"owner": "jingweno", "repo": "octokat"}) assert.Equal(t, nil, err) repo, result := client.Repositories(url).One() assert.T(t, !result.HasError()) assert.Equal(t, 10575811, repo.ID) assert.Equal(t, "octokat", repo.Name) assert.Equal(t, "jingweno/octokat", repo.FullName) assert.T(t, !repo.Private) assert.T(t, !repo.Fork) assert.Equal(t, "https://api.github.com/repos/jingweno/octokat", repo.URL) assert.Equal(t, "https://github.com/jingweno/octokat", repo.HTMLURL) assert.Equal(t, "https://github.com/jingweno/octokat.git", repo.CloneURL) assert.Equal(t, "git://github.com/jingweno/octokat.git", repo.GitURL) assert.Equal(t, "[email protected]:jingweno/octokat.git", repo.SSHURL) assert.Equal(t, "master", repo.MasterBranch) assert.T(t, !repo.Permissions.Admin) assert.T(t, !repo.Permissions.Push) assert.T(t, repo.Permissions.Pull) }
func TestAuthorizationsService_Create(t *testing.T) { setup() defer tearDown() params := AuthorizationParams{Scopes: []string{"public_repo"}} mux.HandleFunc("/authorizations", func(w http.ResponseWriter, r *http.Request) { var authParams AuthorizationParams json.NewDecoder(r.Body).Decode(&authParams) assert.T(t, reflect.DeepEqual(authParams, params)) testMethod(t, r, "POST") respondWithJSON(w, loadFixture("create_authorization.json")) }) url, err := AuthorizationsURL.Expand(nil) assert.Equal(t, nil, err) auth, _ := client.Authorizations(url).Create(params) assert.Equal(t, 3844190, auth.ID) assert.Equal(t, "https://api.github.com/authorizations/3844190", auth.URL) assert.Equal(t, "123", auth.Token) assert.Equal(t, "", auth.Note) assert.Equal(t, "", auth.NoteURL) assert.Equal(t, "2013-09-28 18:44:39 +0000 UTC", auth.CreatedAt.String()) assert.Equal(t, "2013-09-28 18:44:39 +0000 UTC", auth.UpdatedAt.String()) app := App{ClientID: "00000000000000000000", URL: "http://developer.github.com/v3/oauth/#oauth-authorizations-api", Name: "GitHub API"} assert.Equal(t, app, auth.App) assert.Equal(t, 1, len(auth.Scopes)) scopes := []string{"public_repo"} assert.T(t, reflect.DeepEqual(auth.Scopes, scopes)) }
func TestAuthorizationsService_All(t *testing.T) { setup() defer tearDown() mux.HandleFunc("/authorizations", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") respondWithJSON(w, loadFixture("authorizations.json")) }) url, err := AuthorizationsURL.Expand(nil) assert.Equal(t, nil, err) auths, result := client.Authorizations(url).All() assert.T(t, !result.HasError()) firstAuth := auths[0] assert.Equal(t, 1, firstAuth.ID) assert.Equal(t, "https://api.github.com/authorizations/1", firstAuth.URL) assert.Equal(t, "456", firstAuth.Token) assert.Equal(t, "", firstAuth.Note) assert.Equal(t, "", firstAuth.NoteURL) assert.Equal(t, "2012-11-16 01:05:51 +0000 UTC", firstAuth.CreatedAt.String()) assert.Equal(t, "2013-08-21 03:29:51 +0000 UTC", firstAuth.UpdatedAt.String()) app := App{ClientID: "123", URL: "http://localhost:8080", Name: "Test"} assert.Equal(t, app, firstAuth.App) assert.Equal(t, 2, len(firstAuth.Scopes)) scopes := []string{"repo", "user"} assert.T(t, reflect.DeepEqual(firstAuth.Scopes, scopes)) }
func TestDecode(t *testing.T) { input := ` { "Login": "******" , "Url": "/foo/bar{/arg}" , "_links": { "self": { "href": "/foo/bar{/arg}" } } }` user := &HypermediaUser{} decode(t, input, user) assert.Equal(t, "bob", user.Login) assert.Equal(t, 1, len(user.Links)) hl := user.Url url, err := hl.Expand(M{"arg": "baz"}) if err != nil { t.Errorf("Errors parsing %s: %s", hl, err) } assert.Equal(t, "/foo/bar/baz", url.String()) hl = user.Links["self"].Href url, err = hl.Expand(M{"arg": "baz"}) if err != nil { t.Errorf("Errors parsing %s: %s", hl, err) } assert.Equal(t, "/foo/bar/baz", url.String()) }
func TestEditor_openAndEdit_deleteFileWhenOpeningEditorFails(t *testing.T) { tempFile, _ := ioutil.TempFile("", "editor-test") tempFile.Close() ioutil.WriteFile(tempFile.Name(), []byte("hello"), 0644) editor := Editor{ Program: "memory", File: tempFile.Name(), Topic: "test", openEditor: func(program string, file string) error { assert.Equal(t, "memory", program) assert.Equal(t, tempFile.Name(), file) return fmt.Errorf("error") }, } _, err := os.Stat(tempFile.Name()) assert.Equal(t, nil, err) _, err = editor.openAndEdit() assert.Equal(t, "error using text editor for test message", fmt.Sprintf("%s", err)) // file is removed if there's error _, err = os.Stat(tempFile.Name()) assert.T(t, os.IsNotExist(err)) }
func TestEditor_EditTitleAndBody(t *testing.T) { tempFile, _ := ioutil.TempFile("", "PULLREQ") tempFile.Close() editor := Editor{ Program: "memory", File: tempFile.Name(), CS: "#", openEditor: func(program string, file string) error { assert.Equal(t, "memory", program) assert.Equal(t, tempFile.Name(), file) message := `A title A title continues A body A body continues # comment ` return ioutil.WriteFile(file, []byte(message), 0644) }, } title, body, err := editor.EditTitleAndBody() assert.Equal(t, nil, err) assert.Equal(t, "A title A title continues", title) assert.Equal(t, "A body\nA body continues", body) }
func TestSuccessfulGet_BasicAuth(t *testing.T) { setup() defer tearDown() mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") testHeader(t, r, "Accept", defaultMediaType) testHeader(t, r, "User-Agent", userAgent) testHeader(t, r, "Authorization", "Basic amluZ3dlbm86cGFzc3dvcmQ=") testHeader(t, r, "X-GitHub-OTP", "OTP") respondWithJSON(w, `{"login": "******"}`) }) client = NewClientWith( server.URL, userAgent, BasicAuth{ Login: "******", Password: "******", OneTimePassword: "******", }, nil) req, err := client.NewRequest("foo") assert.Equal(t, nil, err) var output map[string]interface{} _, err = req.Get(&output) assert.Equal(t, nil, err) assert.Equal(t, "octokit", output["login"]) }
func TestGitShow(t *testing.T) { repo := fixtures.SetupTestRepo() defer repo.TearDown() output, err := Show("9b5a719a3d76ac9dc2fa635d9b1f34fd73994c06") assert.Equal(t, nil, err) assert.Equal(t, "First comment\n\nMore comment", output) }
func TestGitEditor(t *testing.T) { repo := fixtures.SetupTestRepo() defer repo.TearDown() SetGlobalConfig("core.editor", "foo") gitEditor, err := Editor() assert.Equal(t, nil, err) assert.Equal(t, "foo", gitEditor) }
func TestBranch_RemoteName(t *testing.T) { lp, _ := LocalRepo() b := Branch{lp, "refs/remotes/origin/master"} assert.Equal(t, "origin", b.RemoteName()) b = Branch{lp, "refs/head/master"} assert.Equal(t, "", b.RemoteName()) }
func TestGitRef(t *testing.T) { repo := fixtures.SetupTestRepo() defer repo.TearDown() ref := "08f4b7b6513dffc6245857e497cfd6101dc47818" gitRef, err := Ref(ref) assert.Equal(t, nil, err) assert.Equal(t, ref, gitRef) }
func TestDecoder_Decode(t *testing.T) { link := `<https://api.github.com/user/repos?page=3&per_page=100>; rel="next", <https://api.github.com/user/repos?page=50&per_page=100>; rel="last"` header := http.Header{} header.Add("Link", link) decoder := Decoder{} mediaHeader := decoder.Decode(header) assert.Equal(t, "https://api.github.com/user/repos?page=3&per_page=100", string(mediaHeader.Relations["next"])) assert.Equal(t, "https://api.github.com/user/repos?page=50&per_page=100", string(mediaHeader.Relations["last"])) }
func TestGitRefList(t *testing.T) { repo := fixtures.SetupTestRepo() defer repo.TearDown() refList, err := RefList("08f4b7b6513dffc6245857e497cfd6101dc47818", "9b5a719a3d76ac9dc2fa635d9b1f34fd73994c06") assert.Equal(t, nil, err) assert.Equal(t, 1, len(refList)) assert.Equal(t, "9b5a719a3d76ac9dc2fa635d9b1f34fd73994c06", refList[0]) }
func TestResolveWithHeader(t *testing.T) { client, err := NewFromString("http://api.github.com", nil) if err != nil { t.Fatal(err.Error()) } client.Header.Set("Cache-Control", "private") req, _ := client.NewRequest("") assert.Equal(t, 1, len(req.Header)) assert.Equal(t, "private", req.Header.Get("Cache-Control")) }
func TestGitHubRepo_OriginRemote(t *testing.T) { repo := fixtures.SetupTestRepo() defer repo.TearDown() localRepo, _ := LocalRepo() gitRemote, _ := localRepo.OriginRemote() assert.Equal(t, "origin", gitRemote.Name) u, _ := url.Parse(repo.Remote) assert.Equal(t, u, gitRemote.URL) }
func TestClientRel(t *testing.T) { setup() defer tearDown() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") respondWithJSON(w, loadFixture("root.json")) }) u, err := client.Rel("user", M{"user": "******"}) assert.Equal(t, nil, err) assert.Equal(t, "https://api.github.com/users/root", u.String()) }
func TestErrorResponse(t *testing.T) { setup := Setup(t) defer setup.Teardown() setup.Mux.HandleFunc("/404", func(w http.ResponseWriter, r *http.Request) { head := w.Header() head.Set("Content-Type", "application/json") w.WriteHeader(http.StatusNotFound) w.Write([]byte(`{"message": "not found"}`)) }) client := setup.Client user := &TestUser{} apierr := &TestError{} req, err := client.NewRequest("404") if err != nil { t.Fatalf("request errored: %s", err) } res := req.Get() assert.Equal(t, true, res.IsApiError()) assert.Equal(t, false, res.IsError()) assert.Equal(t, nil, res.Decode(apierr)) assert.Equal(t, 404, res.StatusCode) assert.Equal(t, 0, user.Id) assert.Equal(t, "", user.Login) assert.Equal(t, "not found", apierr.Message) assert.Equal(t, true, res.BodyClosed) }
func TestGitHubRepo_remotesForPublish(t *testing.T) { url, _ := url.Parse("ssh://[email protected]/Owner/repo.git") remotes := []Remote{ { Name: "Owner", URL: url, }, } repo := GitHubRepo{remotes} remotesForPublish := repo.remotesForPublish("owner") assert.Equal(t, 1, len(remotesForPublish)) assert.Equal(t, "Owner", remotesForPublish[0].Name) assert.Equal(t, url.String(), remotesForPublish[0].URL.String()) }
func TestVendorType(t *testing.T) { m := Get(t, "application/vnd.json+xml; charset=utf-8") assert.Equal(t, "application/vnd.json+xml", m.Type) assert.Equal(t, "application", m.MainType) assert.Equal(t, "vnd.json", m.SubType) assert.Equal(t, "xml", m.Suffix) assert.Equal(t, "json", m.Vendor) assert.Equal(t, "xml", m.Format) assert.Equal(t, true, m.IsVendor()) assert.Equal(t, 1, len(m.Params)) assert.Equal(t, "utf-8", m.Params["charset"]) }
func TestSimpleTypeWithParams(t *testing.T) { m := Get(t, "text/plain; charset=utf-8") assert.Equal(t, "text/plain", m.Type) assert.Equal(t, "text", m.MainType) assert.Equal(t, "plain", m.SubType) assert.Equal(t, "", m.Suffix) assert.Equal(t, "", m.Vendor) assert.Equal(t, "", m.Format) assert.Equal(t, false, m.IsVendor()) assert.Equal(t, 1, len(m.Params)) assert.Equal(t, "utf-8", m.Params["charset"]) }
func TestParsesEmptyType(t *testing.T) { m := Get(t, "*; q=.2") assert.Equal(t, "*", m.Type) assert.Equal(t, "*", m.MainType) assert.Equal(t, "", m.SubType) assert.Equal(t, "", m.Suffix) assert.Equal(t, "", m.Vendor) assert.Equal(t, "", m.Format) assert.Equal(t, false, m.IsVendor()) assert.Equal(t, 1, len(m.Params)) assert.Equal(t, ".2", m.Params["q"]) }
func checkSavedReportCrashOption(t *testing.T, always bool, confirm, expected string) { repo := fixtures.SetupTestRepo() defer repo.TearDown() saveReportConfiguration(confirm, always) assert.Equal(t, expected, reportCrashConfig()) }
func TestRootService_One(t *testing.T) { setup() defer tearDown() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") respondWithJSON(w, loadFixture("root.json")) }) url, err := RootURL.Expand(nil) assert.Equal(t, nil, err) root, result := client.Root(url).One() assert.T(t, !result.HasError()) assert.Equal(t, "https://api.github.com/users/{user}", string(root.UserURL)) }