github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/generator/urlbuilder_test.go (about)

     1  package generator
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestURLBuilder_SimplePathParams(t *testing.T) {
    12  	gen, err := opBuilder("simplePathParams", "../fixtures/codegen/todolist.url.simple.yml")
    13  	require.NoError(t, err)
    14  
    15  	op, err := gen.MakeOperation()
    16  	require.NoError(t, err)
    17  
    18  	buf := bytes.NewBuffer(nil)
    19  	opts := opts()
    20  	err = templates.MustGet("serverUrlbuilder").Execute(buf, op)
    21  	require.NoError(t, err)
    22  
    23  	ff, err := opts.LanguageOpts.FormatContent("simple_path_params.go", buf.Bytes())
    24  	require.NoErrorf(t, err, buf.String())
    25  
    26  	res := string(ff)
    27  	assertInCode(t, "var _path = \"/singleValuePath/{siString}/{siInt}/{siInt32}/{siInt64}/{siFloat}/{siFloat32}/{siFloat64}/{siBool}\"", res)
    28  	assertInCode(t, "siBool := swag.FormatBool(o.SiBool)", res)
    29  	assertInCode(t, `if siBool != ""`, res)
    30  	assertInCode(t, `_path = strings.Replace(_path, "{siBool}", siBool, -1)`, res)
    31  	assertInCode(t, `return nil, errors.New("siBool is required on SimplePathParamsURL")`, res)
    32  	assertInCode(t, `siFloat := swag.FormatFloat64(o.SiFloat)`, res)
    33  	assertInCode(t, `if siFloat != ""`, res)
    34  	assertInCode(t, `_path = strings.Replace(_path, "{siFloat}", siFloat, -1)`, res)
    35  	assertInCode(t, `siFloat32 := swag.FormatFloat32(o.SiFloat32)`, res)
    36  	assertInCode(t, `if siFloat32 != "" `, res)
    37  	assertInCode(t, `_path = strings.Replace(_path, "{siFloat32}", siFloat32, -1)`, res)
    38  	assertInCode(t, `return nil, errors.New("siFloat32 is required on SimplePathParamsURL")`, res)
    39  	assertInCode(t, `siFloat64 := swag.FormatFloat64(o.SiFloat64)`, res)
    40  	assertInCode(t, `if siFloat64 != ""`, res)
    41  	assertInCode(t, `_path = strings.Replace(_path, "{siFloat64}", siFloat64, -1)`, res)
    42  	assertInCode(t, `return nil, errors.New("siFloat64 is required on SimplePathParamsURL")`, res)
    43  	assertInCode(t, `siInt := swag.FormatInt64(o.SiInt)`, res)
    44  	assertInCode(t, `if siInt != ""`, res)
    45  	assertInCode(t, `_path = strings.Replace(_path, "{siInt}", siInt, -1)`, res)
    46  	assertInCode(t, `return nil, errors.New("siInt is required on SimplePathParamsURL")`, res)
    47  	assertInCode(t, `siInt32 := swag.FormatInt32(o.SiInt32)`, res)
    48  	assertInCode(t, `if siInt32 != ""`, res)
    49  	assertInCode(t, `_path = strings.Replace(_path, "{siInt32}", siInt32, -1)`, res)
    50  	assertInCode(t, `return nil, errors.New("siInt32 is required on SimplePathParamsURL")`, res)
    51  	assertInCode(t, `siInt64 := swag.FormatInt64(o.SiInt64)`, res)
    52  	assertInCode(t, `if siInt64 != ""`, res)
    53  	assertInCode(t, `_path = strings.Replace(_path, "{siInt64}", siInt64, -1)`, res)
    54  	assertInCode(t, `return nil, errors.New("siInt64 is required on SimplePathParamsURL")`, res)
    55  	assertInCode(t, `siString := o.SiString`, res)
    56  	assertInCode(t, `if siString != ""`, res)
    57  	assertInCode(t, `_path = strings.Replace(_path, "{siString}", siString, -1)`, res)
    58  	assertInCode(t, `return nil, errors.New("siString is required on SimplePathParamsURL")`, res)
    59  	assertInCode(t, `result.Path = golangswaggerpaths.Join(_basePath, _path)`, res)
    60  	assertNotInCode(t, `result.RawQuery = qs.Encode()`, res)
    61  }
    62  
    63  func TestURLBuilder_SimpleQueryParams(t *testing.T) {
    64  	gen, err := opBuilder("simpleQueryParams", "../fixtures/codegen/todolist.url.simple.yml")
    65  	require.NoError(t, err)
    66  
    67  	op, err := gen.MakeOperation()
    68  	require.NoError(t, err)
    69  
    70  	buf := bytes.NewBuffer(nil)
    71  	opts := opts()
    72  	err = templates.MustGet("serverUrlbuilder").Execute(buf, op)
    73  	require.NoError(t, err)
    74  
    75  	ff, err := opts.LanguageOpts.FormatContent("simple_query_params.go", buf.Bytes())
    76  	require.NoErrorf(t, err, buf.String())
    77  
    78  	res := string(ff)
    79  	assertInCode(t, "var _path = \"/singleValueQuery/{id}\"", res)
    80  	assertInCode(t, `id := swag.FormatInt32(o.ID)`, res)
    81  	assertInCode(t, `if id != ""`, res)
    82  	assertInCode(t, `_path = strings.Replace(_path, "{id}", id, -1)`, res)
    83  	assertInCode(t, `return nil, errors.New("id is required on SimpleQueryParamsURL")`, res)
    84  	assertInCode(t, `result.Path = golangswaggerpaths.Join(_basePath, _path)`, res)
    85  	assertInCode(t, `qs := make(url.Values)`, res)
    86  	assertInCode(t, `siBoolQ := swag.FormatBool(o.SiBool)`, res)
    87  	assertInCode(t, `if siBoolQ != ""`, res)
    88  	assertInCode(t, `qs.Set("siBool", siBoolQ)`, res)
    89  	assertInCode(t, `var siFloatQ string`, res)
    90  	assertInCode(t, `if o.SiFloat != nil`, res)
    91  	assertInCode(t, `siFloatQ = swag.FormatFloat64(*o.SiFloat)`, res)
    92  	assertInCode(t, `if siFloatQ != ""`, res)
    93  	assertInCode(t, `qs.Set("siFloat", siFloatQ)`, res)
    94  	assertInCode(t, `var siFloat32Q string`, res)
    95  	assertInCode(t, `if o.SiFloat32 != nil {`, res)
    96  	assertInCode(t, `siFloat32Q = swag.FormatFloat32(*o.SiFloat32)`, res)
    97  	assertInCode(t, `if siFloat32Q != ""`, res)
    98  	assertInCode(t, `qs.Set("siFloat32", siFloat32Q)`, res)
    99  	assertInCode(t, `siFloat64Q := swag.FormatFloat64(o.SiFloat64)`, res)
   100  	assertInCode(t, `if siFloat64Q != ""`, res)
   101  	assertInCode(t, `qs.Set("siFloat64", siFloat64Q)`, res)
   102  	assertInCode(t, `var siIntQ string`, res)
   103  	assertInCode(t, `if o.SiInt != nil`, res)
   104  	assertInCode(t, `siIntQ = swag.FormatInt64(*o.SiInt)`, res)
   105  	assertInCode(t, `if siIntQ != ""`, res)
   106  	assertInCode(t, `qs.Set("siInt", siIntQ)`, res)
   107  	assertInCode(t, `var siInt32Q string`, res)
   108  	assertInCode(t, `if o.SiInt32 != nil`, res)
   109  	assertInCode(t, `siInt32Q = swag.FormatInt32(*o.SiInt32)`, res)
   110  	assertInCode(t, `if siInt32Q != ""`, res)
   111  	assertInCode(t, `qs.Set("siInt32", siInt32Q)`, res)
   112  	assertInCode(t, `siInt64Q := swag.FormatInt64(o.SiInt64)`, res)
   113  	assertInCode(t, `if siInt64Q != ""`, res)
   114  	assertInCode(t, `qs.Set("siInt64", siInt64Q)`, res)
   115  	assertInCode(t, `siStringQ := o.SiString`, res)
   116  	assertInCode(t, `if siStringQ != ""`, res)
   117  	assertInCode(t, `qs.Set("siString", siStringQ)`, res)
   118  	assertInCode(t, `result.RawQuery = qs.Encode()`, res)
   119  }
   120  
   121  func TestURLBuilder_ArrayQueryParams(t *testing.T) {
   122  	testArrayQueryParams(t, "../fixtures/codegen/todolist.url.simple.yml", "")
   123  }
   124  
   125  func TestURLBuilder_ArrayQueryParams_BasePath(t *testing.T) {
   126  	testArrayQueryParams(t, "../fixtures/codegen/todolist.url.basepath.yml", "/v1/api")
   127  }
   128  
   129  func testArrayQueryParams(t testing.TB, filePath, basePath string) {
   130  	gen, err := opBuilder("arrayQueryParams", filePath)
   131  	require.NoError(t, err)
   132  
   133  	op, err := gen.MakeOperation()
   134  	require.NoError(t, err)
   135  
   136  	buf := bytes.NewBuffer(nil)
   137  	opts := opts()
   138  	err = templates.MustGet("serverUrlbuilder").Execute(buf, op)
   139  	require.NoError(t, err)
   140  
   141  	ff, err := opts.LanguageOpts.FormatContent("array_query_params.go", buf.Bytes())
   142  	require.NoErrorf(t, err, buf.String())
   143  
   144  	res := string(ff)
   145  
   146  	assertInCode(t, "var _path = \"/arrayValueQuery/{id}\"", res)
   147  	assertInCode(t, `id := swag.FormatInt32(o.ID)`, res)
   148  	assertInCode(t, `if id != ""`, res)
   149  	assertInCode(t, `_path = strings.Replace(_path, "{id}", id, -1)`, res)
   150  	assertInCode(t, `return nil, errors.New("id is required on ArrayQueryParamsURL")`, res)
   151  	assertInCode(t, "_basePath := o._basePath", res)
   152  
   153  	if basePath != "" {
   154  		assertInCode(t, `if _basePath == ""`, res)
   155  		assertInCode(t, `_basePath = "`+basePath+`"`, res)
   156  	} else {
   157  		assertNotInCode(t, `_basePath = "`+basePath+`"`, res)
   158  	}
   159  
   160  	assertInCode(t, `result.Path = golangswaggerpaths.Join(_basePath, _path)`, res)
   161  	assertInCode(t, `qs := make(url.Values)`, res)
   162  
   163  	assertInCode(t, `var siBoolIR []string`, res)
   164  	assertInCode(t, `for _, siBoolI := range o.SiBool {`, res)
   165  	assertInCode(t, `siBoolIS := swag.FormatBool(siBoolI)`, res)
   166  	assertInCode(t, `if siBoolIS != ""`, res)
   167  	assertInCode(t, `siBoolIR = append(siBoolIR, siBoolIS)`, res)
   168  	assertInCode(t, `siBool := swag.JoinByFormat(siBoolIR, "ssv")`, res)
   169  	assertInCode(t, `if len(siBool) > 0`, res)
   170  	assertInCode(t, `qsv := siBool[0]`, res)
   171  	assertInCode(t, `qs.Set("siBool", qsv)`, res)
   172  
   173  	assertInCode(t, `var siFloatIR []string`, res)
   174  	assertInCode(t, `for _, siFloatI := range o.SiFloat {`, res)
   175  	assertInCode(t, `siFloatIS := swag.FormatFloat64(siFloatI)`, res)
   176  	assertInCode(t, `if siFloatIS != ""`, res)
   177  	assertInCode(t, `siFloatIR = append(siFloatIR, siFloatIS)`, res)
   178  	assertInCode(t, `siFloat := swag.JoinByFormat(siFloatIR, "multi")`, res)
   179  	assertInCode(t, `for _, qsv := range siFloat`, res)
   180  	assertInCode(t, `qs.Add("siFloat", qsv)`, res)
   181  
   182  	assertInCode(t, `var siFloat32IR []string`, res)
   183  	assertInCode(t, `for _, siFloat32I := range o.SiFloat32 {`, res)
   184  	assertInCode(t, `siFloat32IS := swag.FormatFloat32(siFloat32I)`, res)
   185  	assertInCode(t, `if siFloat32IS != ""`, res)
   186  	assertInCode(t, `siFloat32IR = append(siFloat32IR, siFloat32IS)`, res)
   187  	assertInCode(t, `siFloat32 := swag.JoinByFormat(siFloat32IR, "")`, res)
   188  	assertInCode(t, `if len(siFloat32) > 0`, res)
   189  	assertInCode(t, `qsv := siFloat32[0]`, res)
   190  	assertInCode(t, `qs.Set("siFloat32", qsv)`, res)
   191  
   192  	assertInCode(t, `var siFloat64IR []string`, res)
   193  	assertInCode(t, `for _, siFloat64I := range o.SiFloat64 {`, res)
   194  	assertInCode(t, `siFloat64IS := swag.FormatFloat64(siFloat64I)`, res)
   195  	assertInCode(t, `if siFloat64IS != ""`, res)
   196  	assertInCode(t, `siFloat64IR = append(siFloat64IR, siFloat64IS)`, res)
   197  	assertInCode(t, `siFloat64 := swag.JoinByFormat(siFloat64IR, "pipes")`, res)
   198  	assertInCode(t, `if len(siFloat64) > 0`, res)
   199  	assertInCode(t, `qsv := siFloat64[0]`, res)
   200  	assertInCode(t, `qs.Set("siFloat64", qsv)`, res)
   201  
   202  	assertInCode(t, `var siIntIR []string`, res)
   203  	assertInCode(t, `for _, siIntI := range o.SiInt {`, res)
   204  	assertInCode(t, `siIntIS := swag.FormatInt64(siIntI)`, res)
   205  	assertInCode(t, `if siIntIS != ""`, res)
   206  	assertInCode(t, `siIntIR = append(siIntIR, siIntIS)`, res)
   207  	assertInCode(t, `siInt := swag.JoinByFormat(siIntIR, "pipes")`, res)
   208  	assertInCode(t, `if len(siInt) > 0`, res)
   209  	assertInCode(t, `qsv := siInt[0]`, res)
   210  	assertInCode(t, `qs.Set("siInt", qsv)`, res)
   211  
   212  	assertInCode(t, `var siInt32IR []string`, res)
   213  	assertInCode(t, `for _, siInt32I := range o.SiInt32 {`, res)
   214  	assertInCode(t, `siInt32IS := swag.FormatInt32(siInt32I)`, res)
   215  	assertInCode(t, `if siInt32IS != ""`, res)
   216  	assertInCode(t, `siInt32IR = append(siInt32IR, siInt32IS)`, res)
   217  	assertInCode(t, `siInt32 := swag.JoinByFormat(siInt32IR, "tsv")`, res)
   218  	assertInCode(t, `if len(siInt32) > 0`, res)
   219  	assertInCode(t, `qsv := siInt32[0]`, res)
   220  	assertInCode(t, `qs.Set("siInt32", qsv)`, res)
   221  
   222  	assertInCode(t, `var siInt64IR []string`, res)
   223  	assertInCode(t, `for _, siInt64I := range o.SiInt64 {`, res)
   224  	assertInCode(t, `siInt64IS := swag.FormatInt64(siInt64I)`, res)
   225  	assertInCode(t, `if siInt64IS != ""`, res)
   226  	assertInCode(t, `siInt64IR = append(siInt64IR, siInt64IS)`, res)
   227  	assertInCode(t, `siInt64 := swag.JoinByFormat(siInt64IR, "ssv")`, res)
   228  	assertInCode(t, `if len(siInt64) > 0`, res)
   229  	assertInCode(t, `qsv := siInt64[0]`, res)
   230  	assertInCode(t, `qs.Set("siInt64", qsv)`, res)
   231  
   232  	assertInCode(t, `var siStringIR []string`, res)
   233  	assertInCode(t, `for _, siStringI := range o.SiString {`, res)
   234  	assertInCode(t, `siStringIS := siStringI`, res)
   235  	assertInCode(t, `if siStringIS != ""`, res)
   236  	assertInCode(t, `siStringIR = append(siStringIR, siStringIS)`, res)
   237  	assertInCode(t, `siString := swag.JoinByFormat(siStringIR, "csv")`, res)
   238  	assertInCode(t, `if len(siString) > 0`, res)
   239  	assertInCode(t, `qsv := siString[0]`, res)
   240  	assertInCode(t, `qs.Set("siString", qsv)`, res)
   241  
   242  	assertInCode(t, `var siNestedIR []string`, res)
   243  	assertInCode(t, `for _, siNestedI := range o.SiNested`, res)
   244  	assertInCode(t, `var siNestedIIR []string`, res)
   245  	assertInCode(t, `for _, siNestedII := range siNestedI`, res)
   246  	assertInCode(t, `var siNestedIIIR []string`, res)
   247  	assertInCode(t, `for _, siNestedIII := range siNestedII`, res)
   248  	assertInCode(t, `siNestedIIIS := siNestedIII`, res)
   249  	assertInCode(t, `if siNestedIIIS != ""`, res)
   250  	assertInCode(t, `siNestedIIIR = append(siNestedIIIR, siNestedIIIS)`, res)
   251  	assertInCode(t, `siNestedIIS := swag.JoinByFormat(siNestedIIIR, "csv")`, res)
   252  	assertInCode(t, `if len(siNestedIIS) > 0`, res)
   253  	assertInCode(t, `siNestedIISs := siNestedIIS[0]`, res)
   254  	assertInCode(t, `if siNestedIISs != ""`, res)
   255  	assertInCode(t, `siNestedIIR = append(siNestedIIR, siNestedIISs)`, res)
   256  	assertInCode(t, `siNestedIS := swag.JoinByFormat(siNestedIIR, "pipes")`, res)
   257  	assertInCode(t, `if len(siNestedIS) > 0`, res)
   258  	assertInCode(t, `siNestedISs := siNestedIS[0]`, res)
   259  	assertInCode(t, `if siNestedISs != ""`, res)
   260  	assertInCode(t, `siNestedIR = append(siNestedIR, siNestedISs)`, res)
   261  	assertInCode(t, `siNested := swag.JoinByFormat(siNestedIR, "multi")`, res)
   262  	assertInCode(t, `for _, qsv := range siNested`, res)
   263  	assertInCode(t, `qs.Add("siNested", qsv)`, res)
   264  
   265  	assertInCode(t, `result.RawQuery = qs.Encode()`, res)
   266  }
   267  
   268  func TestURLBuilder_Issue2167(t *testing.T) {
   269  	gen, err := opBuilder("xGoNameInParams", "../fixtures/enhancements/2167/swagger.yml")
   270  	require.NoError(t, err)
   271  
   272  	op, err := gen.MakeOperation()
   273  	require.NoError(t, err)
   274  
   275  	buf := bytes.NewBuffer(nil)
   276  	opts := opts()
   277  	err = templates.MustGet("serverUrlbuilder").Execute(buf, op)
   278  	require.NoError(t, err)
   279  
   280  	ff, err := opts.LanguageOpts.FormatContent("get_test_test_name_urlbuilder.go", buf.Bytes())
   281  	require.NoErrorf(t, err, buf.String())
   282  
   283  	res := string(ff)
   284  	assertRegexpInCode(t, `(?m)^\tMyPathName\s+string$`, res)
   285  	assertRegexpInCode(t, `(?m)^\tTestRegion\s+string$`, res)
   286  	assertRegexpInCode(t, `(?m)^\tMyQueryCount\s+\*int64$`, res)
   287  	assertRegexpInCode(t, `(?m)^\tTestLimit\s+\*int64$`, res)
   288  }
   289  
   290  func TestURLBuilder_Issue2167_Error(t *testing.T) {
   291  	gen, err := opBuilder("xGoNameInParams", "../fixtures/enhancements/2167/swagger-error.yml")
   292  	require.NoError(t, err)
   293  
   294  	_, err = gen.MakeOperation()
   295  	require.Error(t, err)
   296  	assert.Contains(t, `GET /test/{test_name}, parameter "test_name": "x-go-name" field must be a string, not a []interface {}`, err.Error())
   297  }