github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/fixtures/bugs/1083/pathparam_test.go (about)

     1  //go:build ignore
     2  
     3  package main
     4  
     5  // this test is designed to be run dynamically by go-swagger test suite
     6  // (generator.generate_test.go), after a test client has been generated.
     7  
     8  import (
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"net/url"
    12  	"testing"
    13  
    14  	"github.com/go-openapi/errors"
    15  	"github.com/go-openapi/loads"
    16  	"github.com/go-openapi/runtime"
    17  	rtclient "github.com/go-openapi/runtime/client"
    18  	"github.com/go-openapi/runtime/middleware"
    19  	"github.com/go-openapi/runtime/middleware/untyped"
    20  	"github.com/go-openapi/swag"
    21  	"github.com/stretchr/testify/require"
    22  	"github.com/thetreep/go-swagger/fixtures/bugs/1083/codegen/client"
    23  	"github.com/thetreep/go-swagger/fixtures/bugs/1083/codegen/client/pet_operations"
    24  	"github.com/thetreep/go-swagger/fixtures/bugs/1083/codegen/models"
    25  )
    26  
    27  type operationHandler struct{}
    28  
    29  func (h *operationHandler) Handle(in interface{}) (interface{}, error) {
    30  	// this handler sends back the input path parameter in Name
    31  
    32  	paramsMap, ok := in.(map[string]interface{})
    33  	if !ok {
    34  		return nil, errors.New(http.StatusInternalServerError, "unexpected params: wants a map")
    35  	}
    36  	val, ok := paramsMap["id"]
    37  	if !ok {
    38  		return nil, errors.New(http.StatusBadRequest, "unexpected params: {id} required")
    39  	}
    40  	id, ok := val.(string)
    41  	if !ok {
    42  		return nil, errors.New(http.StatusBadRequest, "unexpected params: {id} should be a string")
    43  	}
    44  
    45  	return models.Pet{
    46  		ID:   swag.Int64(1),
    47  		Name: swag.String(id),
    48  		Tag:  "test",
    49  	}, nil
    50  }
    51  
    52  func TestEscapedPathParam(t *testing.T) {
    53  	// prepare an untyped test server
    54  	buildTestServer := func(t testing.TB) (string, func()) {
    55  		t.Helper()
    56  		spec, err := loads.Spec("petstore.yaml")
    57  		require.NoError(t, err)
    58  
    59  		api := untyped.NewAPI(spec)
    60  		api.RegisterConsumer("application/json", runtime.JSONConsumer())
    61  		api.RegisterProducer("application/json", runtime.JSONProducer())
    62  		api.RegisterOperation("GET", "/pets/{id}", &operationHandler{})
    63  		handler := middleware.Serve(spec, api)
    64  
    65  		server := httptest.NewServer(handler)
    66  		u, err := url.Parse(server.URL)
    67  		require.NoError(t, err)
    68  
    69  		return u.Host, server.Close
    70  	}
    71  
    72  	buildTestClient := func(t testing.TB, host string) *client.Issue1083 {
    73  		t.Helper()
    74  
    75  		c := client.Default
    76  		tr := rtclient.New(host, "/api", []string{"http"})
    77  		tr.Debug = true
    78  		c.SetTransport(tr)
    79  
    80  		return c
    81  	}
    82  
    83  	submitAndAssert := func(c *client.Issue1083, pathParam string) func(*testing.T) {
    84  		return func(t *testing.T) {
    85  			params := pet_operations.NewGetPetsIDParams().WithID(pathParam)
    86  			resp, err := c.PetOperations.GetPetsID(params)
    87  			require.NoError(t, err)
    88  			require.NotNil(t, resp)
    89  			require.Equal(t, int64(1), swag.Int64Value(resp.Payload.ID))
    90  			require.Equal(t, pathParam, swag.StringValue(resp.Payload.Name))
    91  		}
    92  	}
    93  
    94  	host, clean := buildTestServer(t)
    95  	t.Cleanup(clean)
    96  
    97  	c := buildTestClient(t, host)
    98  
    99  	t.Run("should route with unescaped path param", submitAndAssert(c, "part"))
   100  	t.Run("should route with escaped path param (1)", submitAndAssert(c, "part/ext"))
   101  	t.Run("should route with escaped path param (2)", submitAndAssert(c, "part#ext"))
   102  }