Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

HTTP stub helpers #80

Merged
merged 3 commits into from
Mar 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions octokit/authorizations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ func TestAuthorizationsService_One(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/authorizations/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("authorization.json"))
})
stubGet(t, "/authorizations/1", "authorization", nil)

url, err := AuthorizationsURL.Expand(M{"id": 1})
assert.NoError(t, err)
Expand Down Expand Up @@ -44,10 +41,7 @@ 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"))
})
stubGet(t, "/authorizations", "authorizations", nil)

url, err := AuthorizationsURL.Expand(nil)
assert.NoError(t, err)
Expand Down
5 changes: 1 addition & 4 deletions octokit/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ func TestCommitsService_One(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/repos/octokit/go-octokit/commits/4351fb69b8d5ed075e9cd844e67ad2114b335c82", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("commit.json"))
})
stubGet(t, "/repos/octokit/go-octokit/commits/4351fb69b8d5ed075e9cd844e67ad2114b335c82", "commit", nil)

url, err := CommitsURL.Expand(M{
"owner": "octokit",
Expand Down
6 changes: 1 addition & 5 deletions octokit/emojis_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package octokit

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,10 +10,7 @@ func TestRootEmojisService_All(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("emojis.json"))
})
stubGet(t, "/emojis", "emojis", nil)

url, err := EmojisURL.Expand(nil)
assert.NoError(t, err)
Expand Down
10 changes: 2 additions & 8 deletions octokit/gists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ func TestGistsService_One(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/gists/a6bea192debdbec0d4ab", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("gist.json"))
})
stubGet(t, "/gists/a6bea192debdbec0d4ab", "gist", nil)

url, _ := GistsURL.Expand(M{"gist_id": "a6bea192debdbec0d4ab"})
gist, result := client.Gists(url).One()
Expand All @@ -37,10 +34,7 @@ func TestGistsService_Raw(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/gists/a6bea192debdbec0d4ab", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("gist.json"))
})
stubGet(t, "/gists/a6bea192debdbec0d4ab", "gist", nil)

mux.HandleFunc("/jingweno/a6bea192debdbec0d4ab/raw/80757419d2bd4cfddf7c6be24308eca11b3c330e/grep_cellar", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
Expand Down
11 changes: 2 additions & 9 deletions octokit/git_trees_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package octokit

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,10 +10,7 @@ func TestGitTreesService_One(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.json"))
})
stubGet(t, "/repos/pengwynn/flint/git/trees/master", "tree", nil)

url, err := GitTreesURL.Expand(M{
"owner": "pengwynn",
Expand All @@ -36,10 +32,7 @@ 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"))
})
stubGet(t, "/repos/pengwynn/flint/git/trees/master", "tree_recursive", nil)

url, err := GitTreesURL.Expand(M{
"owner": "pengwynn",
Expand Down
10 changes: 2 additions & 8 deletions octokit/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ func TestIssuesService_All(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/repos/octocat/Hello-World/issues", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("issues.json"))
})
stubGet(t, "/repos/octocat/Hello-World/issues", "issues", nil)

url, err := RepoIssuesURL.Expand(M{"owner": "octocat", "repo": "Hello-World"})
assert.NoError(t, err)
Expand All @@ -32,10 +29,7 @@ func TestIssuesService_One(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/repos/octocat/Hello-World/issues/1347", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("issue.json"))
})
stubGet(t, "/repos/octocat/Hello-World/issues/1347", "issue", nil)

url, err := RepoIssuesURL.Expand(M{"owner": "octocat", "repo": "Hello-World", "number": 1347})
assert.NoError(t, err)
Expand Down
21 changes: 21 additions & 0 deletions octokit/octokit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,24 @@ func loadFixture(f string) string {
c, _ := ioutil.ReadFile(p)
return string(c)
}

func stubGet(t *testing.T, path, fixture string, params map[string]string) {
stubRequest(t, "GET", path, fixture, params)
}

func stubRequest(t *testing.T, method string, path string, fixture string, params map[string]string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could prob. pass a func at the last for assertion or optional manipulation as well:

func stubGet(t *testing.T, path, fixture string, params map[string]string, f func(w http.ResponseWriter, r *http.Request)) {
}


func stubRequest(t *testing.T, method string, path string, fixture string, params map[string]string, f func(w http.ResponseWriter, r *http.Request)) {
  ...

  mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
       if f != nil {
          f(w, r)
       } 
  }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good call. I like that idea.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like that idea as well.

Looking through the code again, there's only a single place where we respond with a fixture and we need other assertions (users_test.go). Other locations with asserts (gists_test.go) respond with an arbitrary string (in this case "hello").
It might be a better idea to pass in the full response rather than just the fixture name.

if mux == nil {
panic(fmt.Errorf("test HTTP server has not been set up"))
}

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, method)
if params != nil {
header := w.Header()
for k, v := range params {
header.Set(k, v)
}
}
respondWithJSON(w, loadFixture(fixture+".json"))
})
}
14 changes: 3 additions & 11 deletions octokit/pull_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ func TestPullRequestService_One(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/repos/octokit/go-octokit/pulls/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("pull_request.json"))
})
stubGet(t, "/repos/octokit/go-octokit/pulls/1", "pull_request", nil)

url, err := PullRequestsURL.Expand(M{"owner": "octokit", "repo": "go-octokit", "number": 1})
assert.NoError(t, err)
Expand Down Expand Up @@ -101,13 +98,8 @@ func TestPullRequestService_All(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/repos/rails/rails/pulls", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
header := w.Header()
link := fmt.Sprintf(`<%s>; rel="next", <%s>; rel="last"`, testURLOf("repositories/8514/pulls?page=2"), testURLOf("repositories/8514/pulls?page=14"))
header.Set("Link", link)
respondWithJSON(w, loadFixture("pull_requests.json"))
})
link := fmt.Sprintf(`<%s>; rel="next", <%s>; rel="last"`, testURLOf("repositories/8514/pulls?page=2"), testURLOf("repositories/8514/pulls?page=14"))
stubGet(t, "/repos/rails/rails/pulls", "pull_requests", map[string]string{"Link": link})

url, err := PullRequestsURL.Expand(M{"owner": "rails", "repo": "rails"})
assert.NoError(t, err)
Expand Down
5 changes: 1 addition & 4 deletions octokit/releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ func TestReleasesService_All(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/repos/jingweno/gh/releases", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("releases.json"))
})
stubGet(t, "/repos/jingweno/gh/releases", "releases", nil)

url, err := ReleasesURL.Expand(M{"owner": "jingweno", "repo": "gh"})
assert.NoError(t, err)
Expand Down
16 changes: 3 additions & 13 deletions octokit/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ 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"))
})
stubGet(t, "/repos/jingweno/octokat", "repository", nil)

url, err := RepositoryURL.Expand(M{"owner": "jingweno", "repo": "octokat"})
assert.NoError(t, err)
Expand Down Expand Up @@ -45,15 +42,8 @@ func TestRepositoresService_All(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/orgs/rails/repos", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")

header := w.Header()
link := fmt.Sprintf(`<%s>; rel="next", <%s>; rel="last"`, testURLOf("organizations/4223/repos?page=2"), testURLOf("organizations/4223/repos?page=3"))
header.Set("Link", link)

respondWithJSON(w, loadFixture("repositories.json"))
})
link := fmt.Sprintf(`<%s>; rel="next", <%s>; rel="last"`, testURLOf("organizations/4223/repos?page=2"), testURLOf("organizations/4223/repos?page=3"))
stubGet(t, "/orgs/rails/repos", "repositories", map[string]string{"Link": link})

url, err := OrgRepositoriesURL.Expand(M{"org": "rails"})
assert.NoError(t, err)
Expand Down
11 changes: 2 additions & 9 deletions octokit/root_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package octokit

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,10 +10,7 @@ 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"))
})
stubGet(t, "/", "root", nil)

url, err := RootURL.Expand(nil)
assert.NoError(t, err)
Expand All @@ -28,10 +24,7 @@ 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"))
})
stubGet(t, "/", "root", nil)

u, err := client.Rel("user", M{"user": "root"})
assert.NoError(t, err)
Expand Down
24 changes: 4 additions & 20 deletions octokit/search_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package octokit

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,10 +10,7 @@ func TestSearchService_Users(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/search/users", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("user_search.json"))
})
stubGet(t, "/search/users", "user_search", nil)

url, err := SearchURL.Expand(map[string]interface{}{
"type": "users",
Expand All @@ -37,11 +33,7 @@ func TestSearchService_Issues(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/search/issues", func(w http.ResponseWriter,
r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("issue_search.json"))
})
stubGet(t, "/search/issues", "issue_search", nil)

url, err := SearchURL.Expand(map[string]interface{}{
"type": "issues",
Expand All @@ -63,11 +55,7 @@ func TestSearchService_Repositories(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/search/repositories", func(w http.ResponseWriter,
r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("repository_search.json"))
})
stubGet(t, "/search/repositories", "repository_search", nil)

url, err := SearchURL.Expand(map[string]interface{}{
"type": "repositories",
Expand All @@ -90,11 +78,7 @@ func TestSearchService_Code(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/search/code", func(w http.ResponseWriter,
r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("code_search.json"))
})
stubGet(t, "/search/code", "code_search", nil)

url, err := SearchURL.Expand(map[string]interface{}{
"type": "code",
Expand Down
6 changes: 1 addition & 5 deletions octokit/statuses_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package octokit

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,10 +10,7 @@ func TestStatuses(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/repos/jingweno/gh/statuses/740211b9c6cd8e526a7124fe2b33115602fbc637", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("statuses.json"))
})
stubGet(t, "/repos/jingweno/gh/statuses/740211b9c6cd8e526a7124fe2b33115602fbc637", "statuses", nil)

sha := "740211b9c6cd8e526a7124fe2b33115602fbc637"
url, err := StatusesURL.Expand(M{"owner": "jingweno", "repo": "gh", "ref": sha})
Expand Down
10 changes: 2 additions & 8 deletions octokit/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ func TestUsersService_GetCurrentUser(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("user.json"))
})
stubGet(t, "/user", "user", nil)

url, _ := CurrentUserURL.Expand(nil)
user, result := client.Users(url).One()
Expand Down Expand Up @@ -59,10 +56,7 @@ func TestUsersService_GetUser(t *testing.T) {
setup()
defer tearDown()

mux.HandleFunc("/users/jingweno", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
respondWithJSON(w, loadFixture("user.json"))
})
stubGet(t, "/users/jingweno", "user", nil)

url, err := UserURL.Expand(M{"user": "jingweno"})
assert.NoError(t, err)
Expand Down