...

Source file src/github.com/go-openapi/runtime/middleware/operation_test.go

Documentation: github.com/go-openapi/runtime/middleware

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package middleware
    16  
    17  import (
    18  	stdcontext "context"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"testing"
    22  
    23  	"github.com/go-openapi/errors"
    24  	"github.com/go-openapi/runtime"
    25  	"github.com/go-openapi/runtime/internal/testing/petstore"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestOperationExecutor(t *testing.T) {
    31  	spec, api := petstore.NewAPI(t)
    32  	api.RegisterOperation("get", "/pets", runtime.OperationHandlerFunc(func(_ interface{}) (interface{}, error) {
    33  		return []interface{}{
    34  			map[string]interface{}{"id": 1, "name": "a dog"},
    35  		}, nil
    36  	}))
    37  
    38  	context := NewContext(spec, api, nil)
    39  	context.router = DefaultRouter(spec, context.api)
    40  	mw := NewOperationExecutor(context)
    41  
    42  	recorder := httptest.NewRecorder()
    43  	request, err := http.NewRequestWithContext(stdcontext.Background(), http.MethodGet, "/api/pets", nil)
    44  	require.NoError(t, err)
    45  	request.Header.Add("Accept", "application/json")
    46  	request.SetBasicAuth("admin", "admin")
    47  	mw.ServeHTTP(recorder, request)
    48  	assert.Equal(t, http.StatusOK, recorder.Code)
    49  	assert.Equal(t, `[{"id":1,"name":"a dog"}]`+"\n", recorder.Body.String())
    50  
    51  	spec, api = petstore.NewAPI(t)
    52  	api.RegisterOperation("get", "/pets", runtime.OperationHandlerFunc(func(_ interface{}) (interface{}, error) {
    53  		return nil, errors.New(http.StatusUnprocessableEntity, "expected")
    54  	}))
    55  
    56  	context = NewContext(spec, api, nil)
    57  	context.router = DefaultRouter(spec, context.api)
    58  	mw = NewOperationExecutor(context)
    59  
    60  	recorder = httptest.NewRecorder()
    61  	request, err = http.NewRequestWithContext(stdcontext.Background(), http.MethodGet, "/api/pets", nil)
    62  	require.NoError(t, err)
    63  	request.Header.Add("Accept", "application/json")
    64  	request.SetBasicAuth("admin", "admin")
    65  	mw.ServeHTTP(recorder, request)
    66  	assert.Equal(t, http.StatusUnprocessableEntity, recorder.Code)
    67  	assert.Equal(t, `{"code":422,"message":"expected"}`, recorder.Body.String())
    68  }
    69  

View as plain text