newTestOAuthServer returns a new test server with the expected base64 encoded client ID and secret.
(t testing.TB, expectedAuth func(testing.TB, string))
| 1804 | |
| 1805 | // newTestOAuthServer returns a new test server with the expected base64 encoded client ID and secret. |
| 1806 | func newTestOAuthServer(t testing.TB, expectedAuth func(testing.TB, string)) testOAuthServer { |
| 1807 | var previousAuth string |
| 1808 | tokenTS := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1809 | auth := r.Header.Get("Authorization") |
| 1810 | if auth == "" { |
| 1811 | require.NoErrorf(t, r.ParseForm(), "Failed to parse form") |
| 1812 | auth = r.FormValue("assertion") |
| 1813 | } |
| 1814 | |
| 1815 | expectedAuth(t, auth) |
| 1816 | |
| 1817 | require.NotEqualf(t, auth, previousAuth, "token endpoint called twice") |
| 1818 | previousAuth = auth |
| 1819 | res, _ := json.Marshal(oauth2TestServerResponse{ |
| 1820 | AccessToken: "12345", |
| 1821 | TokenType: "Bearer", |
| 1822 | }) |
| 1823 | w.Header().Add("Content-Type", "application/json") |
| 1824 | _, _ = w.Write(res) |
| 1825 | })) |
| 1826 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1827 | auth := r.Header.Get("Authorization") |
| 1828 | require.Equalf(t, "Bearer 12345", auth, "bad auth, expected %s, got %s", "Bearer 12345", auth) |
| 1829 | fmt.Fprintln(w, "Hello, client") |
| 1830 | })) |
| 1831 | return testOAuthServer{ |
| 1832 | tokenTS: tokenTS, |
| 1833 | ts: ts, |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | func (s *testOAuthServer) url() string { |
| 1838 | return s.ts.URL |
no test coverage detected