Esempio n. 1
0
func (suite *TestSuiteServer) TestWithAuthToken(c *C) {
	token := auth.NewToken(1*time.Hour).Add("api-from-context", true).Add("unrelated-scope", 1)

	testutil.GetWithRequest(c, "http://localhost:7890/simple-get",
		func(req *http.Request) *http.Request {
			token.SetHeader(req.Header, testutil.PrivateKeyFunc)
			return req
		},
		func(resp *http.Response, body []byte) {
			c.Assert(resp.StatusCode, Equals, http.StatusOK)
			c.Assert(body, DeepEquals, []byte("ok"))
		})
	testutil.GetWithRequest(c, "http://localhost:7890/api-from-context",
		func(req *http.Request) *http.Request {
			token.SetHeader(req.Header, testutil.PrivateKeyFunc)
			return req
		},
		func(resp *http.Response, body []byte) {
			c.Assert(resp.StatusCode, Equals, http.StatusOK)
			c.Assert(body, DeepEquals, []byte("ok"))
		})

	// Wrong scope not authorized to call this method
	token = auth.NewToken(1*time.Hour).Add("wrong-scope", true)
	testutil.GetWithRequest(c, "http://localhost:7890/api-from-context",
		func(req *http.Request) *http.Request {
			token.SetHeader(req.Header, testutil.PrivateKeyFunc)
			return req
		},
		func(resp *http.Response, body []byte) {
			c.Assert(resp.StatusCode, Equals, http.StatusUnauthorized)
		})
}
Esempio n. 2
0
func (suite *TestSuiteServer) TestHandleError(c *C) {
	token := auth.NewToken(1*time.Hour).Add("test-server-error", 1)

	testutil.GetWithRequest(c, "http://localhost:7890/server-error",
		func(req *http.Request) *http.Request {
			token.SetHeader(req.Header, testutil.PrivateKeyFunc)
			return req
		},
		func(resp *http.Response, body []byte) {
			c.Log("resp=", resp.StatusCode, "body=", string(body))
			c.Assert(resp.StatusCode, Equals, http.StatusInternalServerError)
			c.Assert(body, DeepEquals, []byte(`{ "error": "server-error" }`))
		})
}