Example #1
0
func TestOperationExecutor(t *testing.T) {
	spec, api := petstore.NewAPI(t)
	api.RegisterOperation("get", "/pets", runtime.OperationHandlerFunc(func(params interface{}) (interface{}, error) {
		return []interface{}{
			map[string]interface{}{"id": 1, "name": "a dog"},
		}, nil
	}))

	context := NewContext(spec, api, nil)
	context.router = DefaultRouter(spec, context.api)
	mw := NewOperationExecutor(context)

	recorder := httptest.NewRecorder()
	request, _ := http.NewRequest("GET", "/pets", nil)
	request.Header.Add("Accept", "application/json")
	request.SetBasicAuth("admin", "admin")
	mw.ServeHTTP(recorder, request)
	assert.Equal(t, 200, recorder.Code)
	assert.Equal(t, `[{"id":1,"name":"a dog"}]`+"\n", recorder.Body.String())

	spec, api = petstore.NewAPI(t)
	api.RegisterOperation("get", "/pets", runtime.OperationHandlerFunc(func(params interface{}) (interface{}, error) {
		return nil, errors.New(422, "expected")
	}))

	context = NewContext(spec, api, nil)
	context.router = DefaultRouter(spec, context.api)
	mw = NewOperationExecutor(context)

	recorder = httptest.NewRecorder()
	request, _ = http.NewRequest("GET", "/pets", nil)
	request.Header.Add("Accept", "application/json")
	request.SetBasicAuth("admin", "admin")
	mw.ServeHTTP(recorder, request)
	assert.Equal(t, 422, recorder.Code)
	assert.Equal(t, `{"code":422,"message":"expected"}`, recorder.Body.String())
}
Example #2
0
	spec, err := loads.Analyzed(json.RawMessage([]byte(swaggerJSON)), "")
	if err != nil {
		return nil, err
	}
	api := untyped.NewAPI(spec)

	api.RegisterOperation("get", "/pets", getAllPets)
	api.RegisterOperation("post", "/pets", createPet)
	api.RegisterOperation("delete", "/pets/{id}", deletePet)
	api.RegisterOperation("get", "/pets/{id}", getPetByID)

	return middleware.Serve(spec, api), nil
}

var getAllPets = runtime.OperationHandlerFunc(func(data interface{}) (interface{}, error) {
	return pets, nil
})

var createPet = runtime.OperationHandlerFunc(func(data interface{}) (interface{}, error) {
	body := data.(map[string]interface{})["pet"].(map[string]interface{})
	return addPet(Pet{
		Name:   body["name"].(string),
		Status: body["status"].(string),
	}), nil
})

var deletePet = runtime.OperationHandlerFunc(func(data interface{}) (interface{}, error) {
	id := data.(map[string]interface{})["id"].(int64)
	removePet(id)
	return nil, nil
})
Example #3
0
	if err != nil {
		return nil, err
	}
	api := untyped.NewAPI(spec)

	api.RegisterOperation("get", "/pets", getAllPets)
	api.RegisterOperation("post", "/pets", createPet)
	api.RegisterOperation("delete", "/pets/{id}", deletePet)
	api.RegisterOperation("get", "/pets/{id}", getPetByID)

	return middleware.Serve(spec, api), nil
}

var getAllPets = runtime.OperationHandlerFunc(func(data interface{}) (interface{}, error) {
	fmt.Println("getAllPets")
	fmt.Printf("%#v\n", data)
	return pets, nil
})
var createPet = runtime.OperationHandlerFunc(func(data interface{}) (interface{}, error) {
	fmt.Println("createPet")
	fmt.Printf("%#v\n", data)
	body := data.(map[string]interface{})["pet"]
	var pet Pet
	if err := swag.FromDynamicJSON(body, &pet); err != nil {
		return nil, err
	}
	addPet(pet)
	return body, nil
})

var deletePet = runtime.OperationHandlerFunc(func(data interface{}) (interface{}, error) {