MCPcopy
hub / github.com/dghubble/gologin / TestGithubHandler

Function TestGithubHandler

github/login_test.go:18–49  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

16)
17
18func TestGithubHandler(t *testing.T) {
19 jsonData := `{"id": 917408, "name": "Alyssa Hacker"}`
20 expectedUser := &github.User{ID: github.Int64(917408), Name: github.String("Alyssa Hacker")}
21 proxyClient, server := newGithubTestServer("", jsonData)
22 defer server.Close()
23
24 // oauth2 Client will use the proxy client's base Transport
25 ctx := context.WithValue(context.Background(), oauth2.HTTPClient, proxyClient)
26 anyToken := &oauth2.Token{AccessToken: "any-token"}
27 ctx = oauth2Login.WithToken(ctx, anyToken)
28
29 config := &oauth2.Config{}
30 success := func(w http.ResponseWriter, req *http.Request) {
31 ctx := req.Context()
32 githubUser, err := UserFromContext(ctx)
33 assert.Nil(t, err)
34 assert.Equal(t, expectedUser, githubUser)
35 fmt.Fprintf(w, "success handler called")
36 }
37 failure := testutils.AssertFailureNotCalled(t)
38
39 // GithubHandler assert that:
40 // - Token is read from the ctx and passed to the GitHub API
41 // - github User is obtained from the GitHub API
42 // - success handler is called
43 // - github User is added to the ctx of the success handler
44 githubHandler := githubHandler(config, false, http.HandlerFunc(success), failure)
45 w := httptest.NewRecorder()
46 req, _ := http.NewRequest("GET", "/", nil)
47 githubHandler.ServeHTTP(w, req.WithContext(ctx))
48 assert.Equal(t, "success handler called", w.Body.String())
49}
50
51func TestGithubHandler_MissingCtxToken(t *testing.T) {
52 config := &oauth2.Config{}

Callers

nothing calls this directly

Calls 4

AssertFailureNotCalledFunction · 0.92
newGithubTestServerFunction · 0.85
githubHandlerFunction · 0.85
UserFromContextFunction · 0.70

Tested by

no test coverage detected