func TestOperationExecutor(t *testing.T) { spec, api := petstore.NewAPI(t) api.RegisterOperation("get", "/pets", httpkit.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", httpkit.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()) }
if err != nil { return nil, err } api := untyped.NewAPI(spec) api.RegisterOperation("getAllPets", getAllPets) api.RegisterOperation("createPet", createPet) api.RegisterOperation("deletePet", deletePet) api.RegisterOperation("getPetById", getPetByID) return middleware.Serve(spec, api), nil } var getAllPets = httpkit.OperationHandlerFunc(func(data interface{}) (interface{}, error) { fmt.Println("getAllPets") pretty.Println(data) return pets, nil }) var createPet = httpkit.OperationHandlerFunc(func(data interface{}) (interface{}, error) { fmt.Println("createPet") pretty.Println(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 = httpkit.OperationHandlerFunc(func(data interface{}) (interface{}, error) {
spec, err := spec.New(json.RawMessage([]byte(swaggerJSON)), "") if err != nil { return nil, err } api := untyped.NewAPI(spec) api.RegisterOperation("findPets", getAllPets) api.RegisterOperation("addPet", createPet) api.RegisterOperation("deletePet", deletePet) api.RegisterOperation("findPetById", getPetByID) return middleware.Serve(spec, api), nil } var getAllPets = httpkit.OperationHandlerFunc(func(data interface{}) (interface{}, error) { return pets, nil }) var createPet = httpkit.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 = httpkit.OperationHandlerFunc(func(data interface{}) (interface{}, error) { id := data.(map[string]interface{})["id"].(int64) removePet(id) return nil, nil })