github.com/kumasuke120/mockuma@v1.1.9/internal/mckmaps/parser_test.go (about) 1 package mckmaps 2 3 import ( 4 "errors" 5 "fmt" 6 "net/http" 7 "path/filepath" 8 "testing" 9 10 "github.com/kumasuke120/mockuma/internal/myhttp" 11 "github.com/kumasuke120/mockuma/internal/myjson" 12 "github.com/kumasuke120/mockuma/internal/myos" 13 "github.com/rs/cors" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 ) 17 18 var mappings = &MockuMappings{Mappings: []*Mapping{ 19 { 20 URI: "/a1", 21 Method: myhttp.MethodPut, 22 Policies: nil, 23 }, 24 { 25 URI: "/a2", 26 Method: myhttp.MethodPost, 27 Policies: nil, 28 }, 29 { 30 URI: "/a1", 31 Method: myhttp.MethodGet, 32 Policies: nil, 33 }, 34 }} 35 36 func TestMockuMappings_GroupMethodsByURI(t *testing.T) { 37 expected := map[string][]myhttp.HTTPMethod{ 38 "/a1": {myhttp.MethodPut, myhttp.MethodGet}, 39 "/a2": {myhttp.MethodPost}, 40 } 41 actual := mappings.GroupMethodsByURI() 42 43 assert.Equal(t, expected, actual) 44 } 45 46 func TestMockuMappings_IsEmpty(t *testing.T) { 47 assert.False(t, mappings.IsEmpty()) 48 assert.True(t, new(MockuMappings).IsEmpty()) 49 } 50 51 func TestError(t *testing.T) { 52 //noinspection GoImportUsedAsName 53 assert := assert.New(t) 54 55 err0 := &loadError{filename: "test.json", err: errors.New("\n\n")} 56 assert.NotNil(err0) 57 assert.Contains(err0.Error(), "test.json") 58 assert.Contains(err0.Error(), "\n\t\n\t") 59 60 err1 := &parserError{ 61 jsonPath: myjson.NewPath("testPath"), 62 filename: "test.json", 63 err: errors.New("test_error\n"), 64 } 65 assert.NotNil(err1) 66 assert.Contains(err1.Error(), "$.testPath") 67 assert.Contains(err1.Error(), "test.json") 68 assert.Contains(err1.Error(), "test_error") 69 assert.Contains(err1.Error(), "test_error\n\t") 70 } 71 72 func TestNewParser(t *testing.T) { 73 p1 := NewParser("123") 74 assert.Equal(t, "123", p1.filename) 75 } 76 77 //noinspection GoImportUsedAsName 78 func TestParser_Parse(t *testing.T) { 79 assert := assert.New(t) 80 require := require.New(t) 81 82 loadedFilenames = nil 83 84 require.Nil(myos.InitWd()) 85 oldWd := myos.GetWd() 86 require.Nil(myos.Chdir(filepath.Join(oldWd, "testdata", "parser"))) 87 88 expectedMappings := []*Mapping{ 89 { 90 URI: "/m1", 91 Method: myhttp.HTTPMethod("RESET"), 92 Policies: []*Policy{ 93 { 94 CmdType: mapPolicyReturns, 95 Returns: &Returns{ 96 StatusCode: myhttp.StatusOK, 97 Body: []byte("m1"), 98 }, 99 }, 100 }, 101 }, 102 { 103 URI: "/m2", 104 Method: myhttp.MethodPost, 105 Policies: []*Policy{ 106 { 107 When: &When{ 108 Params: []*NameValuesPair{ 109 { 110 Name: "p", 111 Values: []string{"1"}, 112 }, 113 }, 114 }, 115 CmdType: mapPolicyReturns, 116 Returns: &Returns{ 117 StatusCode: myhttp.StatusOK, 118 Body: []byte("m2:1"), 119 }, 120 }, 121 { 122 When: &When{ 123 Params: []*NameValuesPair{ 124 { 125 Name: "p", 126 Values: []string{"2"}, 127 }, 128 }, 129 }, 130 CmdType: mapPolicyReturns, 131 Returns: &Returns{ 132 StatusCode: myhttp.StatusOK, 133 Body: []byte("m2:2"), 134 }, 135 }, 136 { 137 When: &When{ 138 Params: []*NameValuesPair{ 139 { 140 Name: "p", 141 Values: []string{"3"}, 142 }, 143 }, 144 }, 145 CmdType: mapPolicyReturns, 146 Returns: &Returns{ 147 StatusCode: myhttp.StatusOK, 148 Body: []byte("m2:3"), 149 }, 150 }, 151 }, 152 }, 153 } 154 155 fn1 := "parser-single.json" 156 path1, e1 := filepath.Abs(fn1) 157 require.Nil(e1) 158 expected1 := &MockuMappings{ 159 Mappings: expectedMappings, 160 Filenames: []string{fn1}, 161 Config: defaultConfig(), 162 } 163 parser1 := NewParser(path1) 164 actual1, e1 := parser1.Parse() 165 if assert.Nil(e1) { 166 assert.Equal(expected1, actual1) 167 } 168 169 fn2 := "parser-multi.json" 170 expected2 := &MockuMappings{ 171 Mappings: expectedMappings, 172 Filenames: []string{fn2, fn1}, 173 Config: &Config{ 174 MatchTrailingSlash: true, 175 CORS: &CORSOptions{ 176 Enabled: true, 177 AllowCredentials: true, 178 MaxAge: 1600, 179 AllowedOrigins: []string{"*"}, 180 AllowedMethods: []myhttp.HTTPMethod{ 181 myhttp.MethodGet, 182 myhttp.MethodPost, 183 }, 184 AllowedHeaders: []string{ 185 myhttp.HeaderOrigin, 186 myhttp.HeaderAccept, 187 myhttp.HeaderXRequestWith, 188 myhttp.HeaderContentType, 189 myhttp.HeaderAccessControlRequestMethod, 190 myhttp.HeaderAccessControlRequestHeaders, 191 }, 192 }, 193 }, 194 } 195 parser2 := NewParser(fn2) 196 actual2, e2 := parser2.Parse() 197 if assert.Nil(e2) { 198 assert.Equal(expected2, actual2) 199 } 200 201 fn3 := "parser-multi-2.json" 202 expected3 := &MockuMappings{ 203 Mappings: expectedMappings, 204 Filenames: []string{fn3, fn1}, 205 Config: &Config{ 206 MatchTrailingSlash: false, 207 CORS: &CORSOptions{ 208 Enabled: true, 209 AllowCredentials: false, 210 MaxAge: 1800, 211 AllowedOrigins: []string{"*"}, 212 AllowedMethods: []myhttp.HTTPMethod{ 213 myhttp.MethodGet, 214 myhttp.MethodPost, 215 myhttp.MethodHead, 216 myhttp.MethodOptions, 217 }, 218 AllowedHeaders: []string{"X-Auth-Token"}, 219 ExposedHeaders: []string{"Content-Length"}, 220 }, 221 }, 222 } 223 parser3 := NewParser(fn3) 224 actual3, e3 := parser3.Parse() 225 if assert.Nil(e3) { 226 assert.Equal(expected3, actual3) 227 } 228 229 fn4 := "parser-multi-3.json" 230 expected4 := &MockuMappings{ 231 Mappings: expectedMappings, 232 Filenames: []string{fn4, fn1}, 233 Config: &Config{ 234 MatchTrailingSlash: false, 235 CORS: defaultEnabledCORS(), 236 }, 237 } 238 parser4 := NewParser(fn4) 239 actual4, e4 := parser4.Parse() 240 if assert.Nil(e4) { 241 assert.Equal(expected4, actual4) 242 } 243 244 fn5 := "parser-multi-4.json" 245 expected5 := &MockuMappings{ 246 Mappings: expectedMappings, 247 Filenames: []string{fn5, fn1}, 248 Config: &Config{ 249 MatchTrailingSlash: false, 250 CORS: defaultDisabledCORS(), 251 }, 252 } 253 parser5 := NewParser(fn5) 254 actual5, e5 := parser5.Parse() 255 if assert.Nil(e5) { 256 assert.Equal(expected5, actual5) 257 } 258 259 fn6 := "parser-multi-5.json" 260 parser6 := NewParser(fn6) 261 _, e6 := parser6.Parse() 262 if assert.NotNil(e6) { 263 ep6 := e6.(*parserError).jsonPath.String() 264 assert.Equal("$.config.cors", ep6) 265 } 266 267 fn7 := "parser-multi-6.json" 268 parser7 := NewParser(fn7) 269 _, e7 := parser7.Parse() 270 assert.NotNil(e7) 271 272 fn8 := "parser-multi-7.json" 273 expected8 := &MockuMappings{ 274 Mappings: expectedMappings, 275 Filenames: []string{fn8, fn1}, 276 Config: &Config{ 277 MatchTrailingSlash: false, 278 CORS: defaultDisabledCORS(), 279 }, 280 } 281 parser8 := NewParser(fn8) 282 actual8, e8 := parser8.Parse() 283 if assert.Nil(e8) { 284 assert.Equal(expected8, actual8) 285 } 286 287 fn9 := "parser-multi-8.json" 288 parser9 := NewParser(fn9) 289 _, e9 := parser9.Parse() 290 if assert.NotNil(e9) { 291 ep9 := e9.(*parserError).jsonPath.String() 292 assert.Equal("$.config", ep9) 293 } 294 295 fn10 := "parser-multi-9.json" 296 expected10 := &MockuMappings{ 297 Mappings: expectedMappings, 298 Filenames: []string{fn10, fn1}, 299 Config: &Config{ 300 MatchTrailingSlash: true, 301 CORS: defaultDisabledCORS(), 302 }, 303 } 304 parser10 := NewParser(fn10) 305 actual10, e10 := parser10.Parse() 306 if assert.Nil(e10) { 307 assert.Equal(expected10, actual10) 308 } 309 310 require.Nil(myos.Chdir(oldWd)) 311 } 312 313 func TestParser_sortMappings(t *testing.T) { 314 //noinspection GoImportUsedAsName 315 assert := assert.New(t) 316 317 testdata1 := &MockuMappings{Mappings: []*Mapping{ 318 { 319 URI: "/", 320 Method: myhttp.MethodGet, 321 Policies: []*Policy{ 322 {}, 323 }, 324 }, 325 { 326 URI: "/", 327 Method: myhttp.MethodGet, 328 Policies: []*Policy{ 329 {}, 330 }, 331 }, 332 { 333 URI: "/", 334 Method: myhttp.MethodPost, 335 Policies: []*Policy{ 336 {}, 337 }, 338 }, 339 }} 340 expected1 := &MockuMappings{Mappings: []*Mapping{ 341 { 342 URI: "/", 343 Method: myhttp.MethodGet, 344 Policies: []*Policy{ 345 testdata1.Mappings[0].Policies[0], 346 testdata1.Mappings[1].Policies[0], 347 }, 348 }, 349 { 350 URI: "/", 351 Method: myhttp.MethodPost, 352 Policies: []*Policy{ 353 testdata1.Mappings[2].Policies[0], 354 }, 355 }, 356 }} 357 p1 := &Parser{filename: ""} 358 p1.sortMappings(testdata1) 359 assert.Equal(expected1, testdata1) 360 } 361 362 func TestCORSOptions_ToCors(t *testing.T) { 363 //noinspection GoImportUsedAsName 364 assert := assert.New(t) 365 366 co0 := defaultDisabledCORS() 367 assert.Nil(co0.ToCors()) 368 369 co1 := defaultEnabledCORS() 370 expect1 := cors.New(cors.Options{ 371 AllowCredentials: co1.AllowCredentials, 372 MaxAge: int(co1.MaxAge), 373 AllowedOrigins: nil, 374 AllowOriginFunc: anyStrToTrue, 375 AllowedMethods: myhttp.MethodsToStringSlice(co1.AllowedMethods), 376 AllowedHeaders: co1.AllowedHeaders, 377 ExposedHeaders: co1.ExposedHeaders, 378 }) 379 // normal reflect.DeepEqual won't pass 380 assert.Equal(fmt.Sprintf("%v", expect1), fmt.Sprintf("%v", co1.ToCors())) 381 382 co2 := defaultEnabledCORS() 383 co2.AllowedMethods = []myhttp.HTTPMethod{myhttp.MethodGet} 384 co2.AllowCredentials = false 385 co2.AllowedOrigins = []string{"*"} 386 expect2 := cors.New(cors.Options{ 387 AllowCredentials: co2.AllowCredentials, 388 MaxAge: int(co2.MaxAge), 389 AllowedOrigins: co2.AllowedOrigins, 390 AllowedMethods: []string{http.MethodGet, http.MethodOptions}, 391 AllowedHeaders: co2.AllowedHeaders, 392 ExposedHeaders: co2.ExposedHeaders, 393 }) 394 assert.Equal(fmt.Sprintf("%v", expect2), fmt.Sprintf("%v", co2.ToCors())) 395 }