github.com/echohead/hub@v2.2.1+incompatible/github/client_test.go (about) 1 package github 2 3 import ( 4 "fmt" 5 "net/http" 6 "regexp" 7 "testing" 8 9 "github.com/github/hub/Godeps/_workspace/src/github.com/bmizerany/assert" 10 "github.com/github/hub/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit" 11 ) 12 13 func TestClient_newOctokitClient(t *testing.T) { 14 c := NewClient("github.com") 15 cc := c.newOctokitClient(nil) 16 assert.Equal(t, "https://api.github.com", cc.Endpoint.String()) 17 18 c = NewClient("github.corporate.com") 19 cc = c.newOctokitClient(nil) 20 assert.Equal(t, "https://github.corporate.com", cc.Endpoint.String()) 21 22 c = NewClient("http://github.corporate.com") 23 cc = c.newOctokitClient(nil) 24 assert.Equal(t, "http://github.corporate.com", cc.Endpoint.String()) 25 } 26 27 func TestClient_FormatError(t *testing.T) { 28 e := &octokit.ResponseError{ 29 Response: &http.Response{ 30 StatusCode: 401, 31 Status: "401 Not Found", 32 }, 33 } 34 35 err := FormatError("action", e) 36 assert.Equal(t, "Error action: Not Found (HTTP 401)", fmt.Sprintf("%s", err)) 37 38 e = &octokit.ResponseError{ 39 Response: &http.Response{ 40 StatusCode: 422, 41 Status: "422 Unprocessable Entity", 42 }, 43 Message: "error message", 44 } 45 err = FormatError("action", e) 46 assert.Equal(t, "Error action: Unprocessable Entity (HTTP 422)\nerror message", fmt.Sprintf("%s", err)) 47 } 48 49 func TestClient_warnExistenceOfRepo(t *testing.T) { 50 project := &Project{ 51 Name: "hub", 52 Owner: "github", 53 Host: "github.com", 54 } 55 e := &octokit.ResponseError{ 56 Response: &http.Response{ 57 StatusCode: 404, 58 Status: "404 Not Found", 59 }, 60 Message: "error message", 61 } 62 63 err := warnExistenceOfRepo(project, e) 64 assert.Equal(t, "Are you sure that github.com/github/hub exists?", fmt.Sprintf("%s", err)) 65 } 66 67 func TestAuthTokenNote(t *testing.T) { 68 note, err := authTokenNote(1) 69 assert.Equal(t, nil, err) 70 71 reg := regexp.MustCompile("hub for (.+)@(.+)") 72 assert.T(t, reg.MatchString(note)) 73 74 note, err = authTokenNote(2) 75 assert.Equal(t, nil, err) 76 77 reg = regexp.MustCompile("hub for (.+)@(.+) 2") 78 assert.T(t, reg.MatchString(note)) 79 80 }