github.com/yandex/pandora@v0.5.32/components/providers/http/decoders/uripost_test.go (about) 1 package decoders 2 3 import ( 4 "context" 5 "net/http" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 "github.com/yandex/pandora/components/providers/http/config" 13 "github.com/yandex/pandora/components/providers/http/decoders/ammo" 14 ) 15 16 const uripostDecoderInput = `5 /0 17 class 18 [A:b] 19 5 /1 20 class 21 [Host : example.com] 22 [ C : d ] 23 10 /2 24 classclass 25 [A:] 26 [Host : other.net] 27 28 15 /3 wantTag 29 classclassclass 30 ` 31 32 func getUripostAmmoWants(t *testing.T) []DecodedAmmo { 33 var mustNewAmmo = func(t *testing.T, method string, url string, body []byte, header http.Header, tag string) *ammo.Ammo { 34 a := ammo.Ammo{} 35 err := a.Setup(method, url, body, header, tag) 36 require.NoError(t, err) 37 return &a 38 } 39 return []DecodedAmmo{ 40 mustNewAmmo(t, "POST", "/0", []byte("class"), http.Header{"Content-Type": []string{"application/json"}}, ""), 41 mustNewAmmo(t, "POST", "/1", []byte("class"), http.Header{"A": []string{"b"}, "Content-Type": []string{"application/json"}}, ""), 42 mustNewAmmo(t, "POST", "/2", []byte("classclass"), http.Header{"Host": []string{"example.com"}, "A": []string{"b"}, "C": []string{"d"}, "Content-Type": []string{"application/json"}}, ""), 43 mustNewAmmo(t, "POST", "/3", []byte("classclassclass"), http.Header{"Host": []string{"other.net"}, "A": []string{""}, "C": []string{"d"}, "Content-Type": []string{"application/json"}}, "wantTag"), 44 } 45 } 46 47 func Test_uripostDecoder_Scan(t *testing.T) { 48 49 decoder := newURIPostDecoder(strings.NewReader(uripostDecoderInput), config.Config{ 50 Limit: 8, 51 }, http.Header{"Content-Type": []string{"application/json"}}) 52 53 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second) 54 defer cancel() 55 56 wants := getUripostAmmoWants(t) 57 for j := 0; j < 2; j++ { 58 for i, want := range wants { 59 ammo, err := decoder.Scan(ctx) 60 assert.NoError(t, err, "iteration %d-%d", j, i) 61 assert.Equal(t, want, ammo, "iteration %d-%d", j, i) 62 } 63 } 64 65 _, err := decoder.Scan(ctx) 66 assert.Equal(t, err, ErrAmmoLimit) 67 assert.Equal(t, decoder.ammoNum, uint(len(wants)*2)) 68 assert.Equal(t, decoder.passNum, uint(1)) 69 } 70 71 func Test_uripostDecoder_LoadAmmo(t *testing.T) { 72 decoder := newURIPostDecoder(strings.NewReader(uripostDecoderInput), config.Config{ 73 Limit: 8, 74 }, http.Header{"Content-Type": []string{"application/json"}}) 75 76 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 77 defer cancel() 78 79 wants := getUripostAmmoWants(t) 80 81 ammos, err := decoder.LoadAmmo(ctx) 82 assert.NoError(t, err) 83 assert.Equal(t, wants, ammos) 84 assert.Equal(t, decoder.config.Limit, uint(8)) 85 assert.Equal(t, decoder.config.Passes, uint(0)) 86 } 87 88 func Benchmark_uripostDecoder_Scan(b *testing.B) { 89 decoder := newURIPostDecoder( 90 strings.NewReader(uripostDecoderInput), config.Config{}, 91 http.Header{"Content-Type": []string{"application/json"}}, 92 ) 93 ctx := context.Background() 94 95 b.ResetTimer() 96 for i := 0; i < b.N; i++ { 97 _, err := decoder.Scan(ctx) 98 require.NoError(b, err) 99 } 100 }