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