...

Source file src/github.com/go-openapi/runtime/middleware/spec_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  	"context"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/go-openapi/runtime"
    27  	"github.com/go-openapi/runtime/internal/testing/petstore"
    28  )
    29  
    30  func TestServeSpecMiddleware(t *testing.T) {
    31  	spec, api := petstore.NewAPI(t)
    32  	ctx := NewContext(spec, api, nil)
    33  
    34  	t.Run("Spec handler", func(t *testing.T) {
    35  		handler := Spec("", ctx.spec.Raw(), nil)
    36  
    37  		t.Run("serves spec", func(t *testing.T) {
    38  			request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/swagger.json", nil)
    39  			require.NoError(t, err)
    40  			request.Header.Add(runtime.HeaderContentType, runtime.JSONMime)
    41  			recorder := httptest.NewRecorder()
    42  
    43  			handler.ServeHTTP(recorder, request)
    44  			assert.Equal(t, http.StatusOK, recorder.Code)
    45  
    46  			responseHeaders := recorder.Result().Header //nolint:bodyclose // false positive from linter
    47  			responseContentType := responseHeaders.Get("Content-Type")
    48  			assert.Equal(t, applicationJSON, responseContentType)
    49  
    50  			responseBody := recorder.Body
    51  			require.NotNil(t, responseBody)
    52  			require.JSONEq(t, string(spec.Raw()), responseBody.String())
    53  		})
    54  
    55  		t.Run("returns 404 when no next handler", func(t *testing.T) {
    56  			request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/api/pets", nil)
    57  			require.NoError(t, err)
    58  			request.Header.Add(runtime.HeaderContentType, runtime.JSONMime)
    59  			recorder := httptest.NewRecorder()
    60  
    61  			handler.ServeHTTP(recorder, request)
    62  			assert.Equal(t, http.StatusNotFound, recorder.Code)
    63  		})
    64  
    65  		t.Run("forwards to next handler for other url", func(t *testing.T) {
    66  			handler = Spec("", ctx.spec.Raw(), http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
    67  				rw.WriteHeader(http.StatusOK)
    68  			}))
    69  			request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/api/pets", nil)
    70  			require.NoError(t, err)
    71  			request.Header.Add(runtime.HeaderContentType, runtime.JSONMime)
    72  			recorder := httptest.NewRecorder()
    73  
    74  			handler.ServeHTTP(recorder, request)
    75  			assert.Equal(t, http.StatusOK, recorder.Code)
    76  		})
    77  	})
    78  
    79  	t.Run("Spec handler with options", func(t *testing.T) {
    80  		handler := Spec("/swagger", ctx.spec.Raw(), nil,
    81  			WithSpecPath("spec"),
    82  			WithSpecDocument("myapi-swagger.json"),
    83  		)
    84  
    85  		t.Run("serves spec", func(t *testing.T) {
    86  			request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/swagger/spec/myapi-swagger.json", nil)
    87  			require.NoError(t, err)
    88  			request.Header.Add(runtime.HeaderContentType, runtime.JSONMime)
    89  			recorder := httptest.NewRecorder()
    90  
    91  			handler.ServeHTTP(recorder, request)
    92  			assert.Equal(t, http.StatusOK, recorder.Code)
    93  		})
    94  
    95  		t.Run("should not find spec there", func(t *testing.T) {
    96  			request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/swagger.json", nil)
    97  			require.NoError(t, err)
    98  			request.Header.Add(runtime.HeaderContentType, runtime.JSONMime)
    99  			recorder := httptest.NewRecorder()
   100  
   101  			handler.ServeHTTP(recorder, request)
   102  			assert.Equal(t, http.StatusNotFound, recorder.Code)
   103  		})
   104  	})
   105  }
   106  

View as plain text