...

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

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

     1  package denco_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  
    11  	"github.com/go-openapi/runtime/middleware/denco"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func testHandlerFunc(w http.ResponseWriter, r *http.Request, params denco.Params) {
    17  	fmt.Fprintf(w, "method: %s, path: %s, params: %v", r.Method, r.URL.Path, params)
    18  }
    19  
    20  func TestMux(t *testing.T) {
    21  	mux := denco.NewMux()
    22  	handler, err := mux.Build([]denco.Handler{
    23  		mux.GET("/", testHandlerFunc),
    24  		mux.GET("/user/:name", testHandlerFunc),
    25  		mux.POST("/user/:name", testHandlerFunc),
    26  		mux.HEAD("/user/:name", testHandlerFunc),
    27  		mux.PUT("/user/:name", testHandlerFunc),
    28  		mux.Handler(http.MethodGet, "/user/handler", testHandlerFunc),
    29  		mux.Handler(http.MethodPost, "/user/handler", testHandlerFunc),
    30  		mux.Handler(http.MethodPut, "/user/inference", testHandlerFunc),
    31  	})
    32  	require.NoError(t, err)
    33  
    34  	server := httptest.NewServer(handler)
    35  	defer server.Close()
    36  
    37  	for _, v := range []struct {
    38  		status                 int
    39  		method, path, expected string
    40  	}{
    41  		{http.StatusOK, http.MethodGet, "/", "method: GET, path: /, params: []"},
    42  		{http.StatusOK, http.MethodGet, "/user/alice", "method: GET, path: /user/alice, params: [{name alice}]"},
    43  		{http.StatusOK, http.MethodPost, "/user/bob", "method: POST, path: /user/bob, params: [{name bob}]"},
    44  		{http.StatusOK, http.MethodHead, "/user/alice", ""},
    45  		{http.StatusOK, http.MethodPut, "/user/bob", "method: PUT, path: /user/bob, params: [{name bob}]"},
    46  		{http.StatusNotFound, http.MethodPost, "/", "404 page not found\n"},
    47  		{http.StatusNotFound, http.MethodGet, "/unknown", "404 page not found\n"},
    48  		{http.StatusNotFound, http.MethodPost, "/user/alice/1", "404 page not found\n"},
    49  		{http.StatusOK, http.MethodGet, "/user/handler", "method: GET, path: /user/handler, params: []"},
    50  		{http.StatusOK, http.MethodPost, "/user/handler", "method: POST, path: /user/handler, params: []"},
    51  		{http.StatusOK, http.MethodPut, "/user/inference", "method: PUT, path: /user/inference, params: []"},
    52  	} {
    53  		req, err := http.NewRequestWithContext(context.Background(), v.method, server.URL+v.path, nil)
    54  		require.NoError(t, err)
    55  
    56  		res, err := http.DefaultClient.Do(req)
    57  		require.NoError(t, err)
    58  
    59  		defer res.Body.Close()
    60  		body, err := io.ReadAll(res.Body)
    61  		require.NoError(t, err)
    62  
    63  		actual := string(body)
    64  		expected := v.expected
    65  
    66  		assert.Equalf(t, v.status, res.StatusCode, "for method %s in path %s", v.method, v.path)
    67  		assert.Equalf(t, expected, actual, "for method %s in path %s", v.method, v.path)
    68  	}
    69  }
    70  
    71  func TestNotFound(t *testing.T) {
    72  	mux := denco.NewMux()
    73  	handler, err := mux.Build([]denco.Handler{})
    74  	require.NoError(t, err)
    75  
    76  	server := httptest.NewServer(handler)
    77  	defer server.Close()
    78  
    79  	origNotFound := denco.NotFound
    80  	defer func() {
    81  		denco.NotFound = origNotFound
    82  	}()
    83  	denco.NotFound = func(w http.ResponseWriter, r *http.Request, params denco.Params) {
    84  		w.WriteHeader(http.StatusServiceUnavailable)
    85  		fmt.Fprintf(w, "method: %s, path: %s, params: %v", r.Method, r.URL.Path, params)
    86  	}
    87  	req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, server.URL, nil)
    88  	require.NoError(t, err)
    89  	res, err := http.DefaultClient.Do(req)
    90  	require.NoError(t, err)
    91  
    92  	defer res.Body.Close()
    93  	body, err := io.ReadAll(res.Body)
    94  	require.NoError(t, err)
    95  
    96  	actual := string(body)
    97  	expected := "method: GET, path: /, params: []"
    98  
    99  	assert.Equal(t, http.StatusServiceUnavailable, res.StatusCode)
   100  	assert.Equal(t, expected, actual)
   101  }
   102  

View as plain text