Exemplo n.º 1
0
func TestSimplePetstoreDeletePet(t *testing.T) {
	handler, _ := NewPetstore()
	// Serves swagger spec document
	r, _ := httpkit.JSONRequest("DELETE", "/api/pets/1", nil)
	rw := httptest.NewRecorder()
	handler.ServeHTTP(rw, r)
	assert.Equal(t, 204, rw.Code)
	assert.Equal(t, "", rw.Body.String())

	r, _ = httpkit.JSONRequest("GET", "/api/pets/1", nil)
	rw = httptest.NewRecorder()
	handler.ServeHTTP(rw, r)
	assert.Equal(t, 404, rw.Code)
	assert.Equal(t, "{\"code\":404,\"message\":\"not found: pet 1\"}", rw.Body.String())
}
Exemplo n.º 2
0
func TestSimplePetstoreAddPet(t *testing.T) {
	handler, _ := NewPetstore()
	// Serves swagger spec document
	r, _ := httpkit.JSONRequest("POST", "/api/pets", bytes.NewBuffer([]byte(`{"name": "Fish","status": "available"}`)))
	rw := httptest.NewRecorder()
	handler.ServeHTTP(rw, r)
	assert.Equal(t, 200, rw.Code)
	assert.Equal(t, "{\"id\":3,\"name\":\"Fish\",\"status\":\"available\"}\n", rw.Body.String())
}
Exemplo n.º 3
0
func TestSimplePetstoreSpec(t *testing.T) {
	handler, _ := NewPetstore()
	// Serves swagger spec document
	r, _ := httpkit.JSONRequest("GET", "/swagger.json", nil)
	rw := httptest.NewRecorder()
	handler.ServeHTTP(rw, r)
	assert.Equal(t, 200, rw.Code)
	assert.Equal(t, swaggerJSON, rw.Body.String())
}
Exemplo n.º 4
0
func TestSimplePetstoreAllPets(t *testing.T) {
	handler, _ := NewPetstore()
	// Serves swagger spec document
	r, _ := httpkit.JSONRequest("GET", "/api/pets", nil)
	rw := httptest.NewRecorder()
	handler.ServeHTTP(rw, r)
	assert.Equal(t, 200, rw.Code)
	assert.Equal(t, "[{\"id\":1,\"name\":\"Dog\",\"status\":\"available\"},{\"id\":2,\"name\":\"Cat\",\"status\":\"pending\"}]\n", rw.Body.String())
}
Exemplo n.º 5
0
func TestContextAuthorize(t *testing.T) {
	spec, api := petstore.NewAPI(t)
	ctx := NewContext(spec, api, nil)
	ctx.router = DefaultRouter(spec, ctx.api)

	request, _ := httpkit.JSONRequest("GET", "/pets", nil)

	v, ok := context.GetOk(request, ctxSecurityPrincipal)
	assert.False(t, ok)
	assert.Nil(t, v)

	ri, ok := ctx.RouteInfo(request)
	assert.True(t, ok)
	p, err := ctx.Authorize(request, ri)
	assert.Error(t, err)
	assert.Nil(t, p)

	v, ok = context.GetOk(request, ctxSecurityPrincipal)
	assert.False(t, ok)
	assert.Nil(t, v)

	request.SetBasicAuth("wrong", "wrong")
	p, err = ctx.Authorize(request, ri)
	assert.Error(t, err)
	assert.Nil(t, p)

	v, ok = context.GetOk(request, ctxSecurityPrincipal)
	assert.False(t, ok)
	assert.Nil(t, v)

	request.SetBasicAuth("admin", "admin")
	p, err = ctx.Authorize(request, ri)
	assert.NoError(t, err)
	assert.Equal(t, "admin", p)

	v, ok = context.GetOk(request, ctxSecurityPrincipal)
	assert.True(t, ok)
	assert.Equal(t, "admin", v)

	request.SetBasicAuth("doesn't matter", "doesn't")
	pp, rr := ctx.Authorize(request, ri)
	assert.Equal(t, p, pp)
	assert.Equal(t, err, rr)
}