github.com/yandex/pandora@v0.5.32/components/providers/http/provider_test.go (about) 1 package http 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/spf13/afero" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 "github.com/yandex/pandora/components/providers/http/config" 11 "github.com/yandex/pandora/components/providers/http/provider" 12 ) 13 14 func TestNewProvider_invalidDecoder(t *testing.T) { 15 fs := afero.NewMemMapFs() 16 conf := config.Config{ 17 Decoder: "invalid", 18 } 19 20 p, err := NewProvider(fs, conf) 21 if p != nil || err == nil { 22 t.Error("expected error when creating provider with invalid decoder type") 23 } 24 } 25 26 func TestNewProvider(t *testing.T) { 27 fs := afero.NewMemMapFs() 28 29 tmpFile, err := fs.Create("ammo") 30 require.NoError(t, err) 31 defer os.Remove(tmpFile.Name()) 32 33 _, err = tmpFile.Write([]byte(" {}\n\n")) // content is important only for jsonDecoder 34 require.NoError(t, err) 35 36 cases := []struct { 37 name string 38 conf config.Config 39 expected config.DecoderType 40 filePath string 41 }{ 42 { 43 name: "URIDecoder", 44 conf: config.Config{Decoder: config.DecoderURI, Uris: []string{"http://example.com"}}, 45 expected: config.DecoderURI, 46 filePath: "", 47 }, 48 { 49 name: "FileDecoder", 50 conf: config.Config{Decoder: config.DecoderURI, File: tmpFile.Name()}, 51 expected: config.DecoderURI, 52 filePath: tmpFile.Name(), 53 }, 54 { 55 name: "DecoderURIPost", 56 conf: config.Config{Decoder: config.DecoderURIPost, File: tmpFile.Name()}, 57 expected: config.DecoderURIPost, 58 filePath: tmpFile.Name(), 59 }, 60 { 61 name: "DecoderRaw", 62 conf: config.Config{Decoder: config.DecoderRaw, File: tmpFile.Name()}, 63 expected: config.DecoderRaw, 64 filePath: tmpFile.Name(), 65 }, 66 { 67 name: "DecoderJSONLine", 68 conf: config.Config{Decoder: config.DecoderJSONLine, File: tmpFile.Name()}, 69 expected: config.DecoderJSONLine, 70 filePath: tmpFile.Name(), 71 }, 72 } 73 74 for _, tc := range cases { 75 t.Run(tc.name, func(t *testing.T) { 76 provdr, err := NewProvider(fs, tc.conf) 77 if err != nil { 78 t.Fatalf("failed to create provider: %s", err) 79 } 80 81 p, ok := provdr.(*provider.Provider) 82 require.True(t, ok) 83 require.NotNil(t, p) 84 defer func() { 85 err := p.Close() 86 require.NoError(t, err) 87 }() 88 89 assert.NotNil(t, p.Sink) 90 assert.Equal(t, fs, p.FS) 91 assert.Equal(t, tc.expected, p.Config.Decoder) 92 assert.Equal(t, tc.filePath, p.Config.File) 93 94 if p.Decoder == nil && tc.expected != "" { 95 t.Error("decoder is nil") 96 } 97 }) 98 } 99 }