Example #1
0
func TestAccountsCurrent(t *testing.T) {
	defer transport.Reset()
	endpoint := RegisterResponder("GET", "http://api.example.com/auth/userinfo",
		func(req *http.Request) (*http.Response, error) {
			account := Account{
				Email: "*****@*****.**",
			}
			return httpmock.NewJsonResponse(200, account)
		},
	)

	account, err := client.Accounts.Current(nil)
	if err != nil {
		t.Fatal(err)
	}

	if !endpoint.Called {
		t.Fatalf("never called API")
	}

	if account == nil {
		t.Fatalf("expected account, got nil")
	}

	if account.Email != "*****@*****.**" {
		t.Errorf("expected email to be \"[email protected]\", got %q", account.Email)
	}
}
Example #2
0
func setupMock() {

	testUser := User{
		EventsURL:         "https://api.github.com/users/octocat/events{/privacy}",
		Followers:         20,
		FollowersURL:      "https://api.github.com/users/octocat/followers",
		Following:         0,
		FollowingURL:      "https://api.github.com/users/octocat/following{/other_user}",
		GistsURL:          "https://api.github.com/users/octocat/gists{/gist_id}",
		Hireable:          false,
		HTMLURL:           "https://github.com/octocat",
		Location:          "San Francisco",
		Login:             "******",
		Name:              "monalisa octocat",
		OrganizationsURL:  "https://api.github.com/users/octocat/orgs",
		PublicRepos:       2,
		ReceivedEventsURL: "https://api.github.com/users/octocat/received_events",
		ReposURL:          "https://api.github.com/users/octocat/repos",
		StarredURL:        "https://api.github.com/users/octocat/starred{/owner}{/repo}",
		SubscriptionsURL:  "https://api.github.com/users/octocat/subscriptions",
		Type:              "User",
		URL:               "https://api.github.com/users/octocat",
	}

	httpmock.RegisterResponder("GET", "http://testapi.my/hello",
		func(req *http.Request) (*http.Response, error) {
			resp := httpmock.NewStringResponse(200, "Hello World!")
			return resp, nil
		},
	)

	httpmock.RegisterResponder("GET", "http://testapi.my/user/octocat",
		func(req *http.Request) (*http.Response, error) {
			return httpmock.NewJsonResponse(200, testUser)
		},
	)

	httpmock.RegisterResponder("GET", "http://testapi.my/user/someveryunknown",
		func(req *http.Request) (*http.Response, error) {
			resp := httpmock.NewStringResponse(404, "user someveryunknown not found")
			return resp, nil
		},
	)

	httpmock.RegisterResponder("GET", "http://testapi.my/user/BadGuy",
		func(req *http.Request) (*http.Response, error) {
			resp := httpmock.NewStringResponse(500, "BadGuy failed me :(")
			return resp, nil
		},
	)

	httpmock.RegisterResponder("POST", "http://testapi.my/user",
		func(req *http.Request) (*http.Response, error) {
			return httpmock.NewJsonResponse(201, testUser)
		},
	)

	httpmock.RegisterResponder("PATCH", "http://testapi.my/user/octocat",
		func(req *http.Request) (*http.Response, error) {
			patchedUser := testUser
			patchedUser.Name = "I Am Updated!"
			return httpmock.NewJsonResponse(200, patchedUser)
		},
	)

	httpmock.RegisterResponder("DELETE", "http://testapi.my/user/octocat",
		func(req *http.Request) (*http.Response, error) {
			resp := httpmock.NewBytesResponse(204, nil)
			return resp, nil
		},
	)

	httpmock.RegisterResponder("DELETE", "http://testapi.my/user/someveryunknown",
		func(req *http.Request) (*http.Response, error) {
			resp := httpmock.NewStringResponse(404, "user someveryunknown not found")
			return resp, nil
		},
	)

	httpmock.RegisterResponder("DELETE", "http://testapi.my/user/BadGuy",
		func(req *http.Request) (*http.Response, error) {
			resp := httpmock.NewStringResponse(500, "BadGuy failed me :(")
			return resp, nil
		},
	)
}