forked from GoCodeAlone/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_test.go
More file actions
660 lines (562 loc) · 21.4 KB
/
module_test.go
File metadata and controls
660 lines (562 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package httpclient
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"github.com/CrisisTextLine/modular"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// MockApplication implements modular.Application interface for testing
type MockApplication struct {
mock.Mock
}
func (m *MockApplication) GetConfigSection(name string) (modular.ConfigProvider, error) {
args := m.Called(name)
if err := args.Error(1); err != nil {
return args.Get(0).(modular.ConfigProvider), fmt.Errorf("failed to get config section %s: %w", name, err)
}
return args.Get(0).(modular.ConfigProvider), nil
}
func (m *MockApplication) RegisterConfigSection(name string, provider modular.ConfigProvider) {
m.Called(name, provider)
}
func (m *MockApplication) Logger() modular.Logger {
args := m.Called()
return args.Get(0).(modular.Logger)
}
func (m *MockApplication) SetLogger(logger modular.Logger) {
m.Called(logger)
}
func (m *MockApplication) ConfigProvider() modular.ConfigProvider {
args := m.Called()
return args.Get(0).(modular.ConfigProvider)
}
func (m *MockApplication) SvcRegistry() modular.ServiceRegistry {
args := m.Called()
return args.Get(0).(modular.ServiceRegistry)
}
func (m *MockApplication) ConfigSections() map[string]modular.ConfigProvider {
args := m.Called()
return args.Get(0).(map[string]modular.ConfigProvider)
}
func (m *MockApplication) RegisterService(name string, service any) error {
args := m.Called(name, service)
if err := args.Error(0); err != nil {
return fmt.Errorf("failed to register service %s: %w", name, err)
}
return nil
}
func (m *MockApplication) GetService(name string, target any) error {
args := m.Called(name, target)
if err := args.Error(0); err != nil {
return fmt.Errorf("failed to get service %s: %w", name, err)
}
return nil
}
// Add other required methods to satisfy the interface
func (m *MockApplication) Name() string { return "mock-app" }
func (m *MockApplication) IsInitializing() bool { return false }
func (m *MockApplication) IsStarting() bool { return false }
func (m *MockApplication) IsStopping() bool { return false }
func (m *MockApplication) RegisterModule(module modular.Module) {}
func (m *MockApplication) Run() error { return nil }
func (m *MockApplication) Shutdown(ctx context.Context) error { return nil }
func (m *MockApplication) Init() error { return nil }
func (m *MockApplication) Start() error { return nil }
func (m *MockApplication) Stop() error { return nil }
// Newly added methods to satisfy updated modular.Application interface
func (m *MockApplication) Context() context.Context { return context.Background() }
func (m *MockApplication) GetServicesByModule(moduleName string) []string { return []string{} }
func (m *MockApplication) GetServiceEntry(serviceName string) (*modular.ServiceRegistryEntry, bool) {
return nil, false
}
func (m *MockApplication) IsVerboseConfig() bool {
return false
}
func (m *MockApplication) SetVerboseConfig(verbose bool) {
// No-op in mock
}
func (m *MockApplication) GetServicesByInterface(interfaceType reflect.Type) []*modular.ServiceRegistryEntry {
return []*modular.ServiceRegistryEntry{}
}
func (m *MockApplication) GetModule(name string) modular.Module {
return nil
}
func (m *MockApplication) GetAllModules() map[string]modular.Module {
return make(map[string]modular.Module)
}
func (m *MockApplication) StartTime() time.Time {
return time.Time{}
}
func (m *MockApplication) OnConfigLoaded(hook func(app modular.Application) error) {}
// MockLogger implements modular.Logger interface for testing
type MockLogger struct {
mock.Mock
}
func (m *MockLogger) Debug(msg string, keyvals ...interface{}) {
m.Called(msg, keyvals)
}
func (m *MockLogger) Info(msg string, keyvals ...interface{}) {
m.Called(msg, keyvals)
}
func (m *MockLogger) Warn(msg string, keyvals ...interface{}) {
m.Called(msg, keyvals)
}
func (m *MockLogger) Error(msg string, keyvals ...interface{}) {
m.Called(msg, keyvals)
}
// MockConfigProvider implements modular.ConfigProvider interface for testing
type MockConfigProvider struct {
mock.Mock
}
func (m *MockConfigProvider) GetConfig() interface{} {
args := m.Called()
return args.Get(0)
}
// TestNewHTTPClientModule tests the creation of a new HTTP client module
func TestNewHTTPClientModule(t *testing.T) {
module := NewHTTPClientModule()
assert.NotNil(t, module, "Module should not be nil")
assert.Equal(t, "httpclient", module.Name(), "Module name should be 'httpclient'")
}
// TestHTTPClientModule_Init tests the initialization of the HTTP client module
func TestHTTPClientModule_Init(t *testing.T) {
// Create mocks
mockApp := new(MockApplication)
mockLogger := new(MockLogger)
mockConfigProvider := new(MockConfigProvider)
// Setup expectations
mockApp.On("Logger").Return(mockLogger)
mockLogger.On("Info", "Initializing HTTP client module", mock.Anything).Return()
mockApp.On("GetConfigSection", "httpclient").Return(mockConfigProvider, nil)
// Setup config
config := &Config{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: 90 * time.Second,
RequestTimeout: 30 * time.Second,
TLSTimeout: 10 * time.Second,
DisableCompression: false,
DisableKeepAlives: false,
Verbose: false,
}
mockConfigProvider.On("GetConfig").Return(config)
// Create and initialize module
module := NewHTTPClientModule().(*HTTPClientModule)
err := module.Init(mockApp)
// Assertions
require.NoError(t, err, "Init should not return an error")
assert.NotNil(t, module.httpClient, "HTTP client should not be nil")
assert.Equal(t, 30*time.Second, module.httpClient.Timeout, "Timeout should be set correctly")
// Verify expectations
mockApp.AssertExpectations(t)
mockLogger.AssertExpectations(t)
mockConfigProvider.AssertExpectations(t)
}
// TestHTTPClientModule_Client tests the Client method
func TestHTTPClientModule_Client(t *testing.T) {
// Create module and manually set the HTTP client
module := &HTTPClientModule{
httpClient: &http.Client{},
}
client := module.Client()
assert.NotNil(t, client, "Client should not be nil")
assert.Equal(t, module.httpClient, client, "Client() should return the module's HTTP client")
}
// TestHTTPClientModule_WithTimeout tests the WithTimeout method
func TestHTTPClientModule_WithTimeout(t *testing.T) {
// Create module and manually set the HTTP client
module := &HTTPClientModule{
httpClient: &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{},
},
}
// Test with a positive timeout
client := module.WithTimeout(60)
assert.NotNil(t, client, "Client should not be nil")
assert.Equal(t, 60*time.Second, client.Timeout, "Timeout should be 60 seconds")
// Test with a negative timeout (should return default client)
client = module.WithTimeout(-1)
assert.NotNil(t, client, "Client should not be nil")
assert.Equal(t, module.httpClient, client, "Should return the default client")
}
// TestHTTPClientModule_RequestModifier tests the request modifier functionality
func TestHTTPClientModule_RequestModifier(t *testing.T) {
// Create module
module := &HTTPClientModule{
modifier: func(r *http.Request) *http.Request {
r.Header.Set("X-Test", "test-value")
return r
},
}
// Create a test request
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://example.com", nil)
// Apply the modifier
modifiedReq := module.RequestModifier()(req)
// Verify the modification
assert.Equal(t, "test-value", modifiedReq.Header.Get("X-Test"), "Header should be set by modifier")
}
// TestHTTPClientModule_SetRequestModifier tests setting a request modifier
func TestHTTPClientModule_SetRequestModifier(t *testing.T) {
// Create module with default modifier
module := &HTTPClientModule{
modifier: func(r *http.Request) *http.Request { return r },
}
// Set a new modifier
module.SetRequestModifier(func(r *http.Request) *http.Request {
r.Header.Set("X-Custom", "custom-value")
return r
})
// Create a test request
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://example.com", nil)
// Apply the modifier
modifiedReq := module.modifier(req)
// Verify the modification
assert.Equal(t, "custom-value", modifiedReq.Header.Get("X-Custom"), "Header should be set by new modifier")
}
// TestHTTPClientModule_LoggingTransport tests the logging transport
func TestHTTPClientModule_LoggingTransport(t *testing.T) {
// Create mocks
mockLogger := new(MockLogger)
// Setup temporary file for logging
tmpDir, err := os.MkdirTemp("", "httpclient-test")
if err != nil {
t.Fatal(err)
}
defer func() {
if removeErr := os.RemoveAll(tmpDir); removeErr != nil {
t.Logf("Failed to remove temp directory: %v", removeErr)
}
}()
fileLogger, err := NewFileLogger(tmpDir, mockLogger)
require.NoError(t, err)
// Setup test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte("Hello, world!")); err != nil {
t.Logf("Failed to write response: %v", err)
}
}))
defer server.Close()
// Create logging transport
mockLogger.On("Info", mock.Anything, mock.Anything).Return()
mockLogger.On("Debug", mock.Anything, mock.Anything).Return().Maybe() // Debug calls are optional with new logging
transport := &loggingTransport{
Transport: http.DefaultTransport,
Logger: mockLogger,
FileLogger: fileLogger,
LogHeaders: true,
LogBody: true,
MaxBodyLogSize: 1024,
LogToFile: true,
}
// Create client with logging transport
client := &http.Client{
Transport: transport,
}
// Make a request
req, _ := http.NewRequestWithContext(context.Background(), "GET", server.URL, nil)
resp, err := client.Do(req)
// Assertions
require.NoError(t, err, "Request should not fail")
assert.NotNil(t, resp, "Response should not be nil")
assert.Equal(t, http.StatusOK, resp.StatusCode, "Status code should be 200")
// Verify logger expectations
mockLogger.AssertExpectations(t)
// Cleanup
if closeErr := resp.Body.Close(); closeErr != nil {
t.Logf("Failed to close response body: %v", closeErr)
}
if closeErr := fileLogger.Close(); closeErr != nil {
t.Logf("Failed to close file logger: %v", closeErr)
}
}
// TestHTTPClientModule_IntegrationWithServer tests the HTTP client talking to a test server
func TestHTTPClientModule_IntegrationWithServer(t *testing.T) {
// Setup test server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check request headers
assert.Equal(t, "test-value", r.Header.Get("X-Test-Header"), "Header should be sent")
// Return a response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"success": true}`)); err != nil {
t.Logf("Failed to write response: %v", err)
}
}))
defer server.Close()
// Create module with custom modifier
module := &HTTPClientModule{
httpClient: &http.Client{},
modifier: func(r *http.Request) *http.Request {
r.Header.Set("X-Test-Header", "test-value")
return r
},
}
// Create request
req, _ := http.NewRequestWithContext(context.Background(), "GET", server.URL, nil)
// Apply modifier and make the request
req = module.RequestModifier()(req)
resp, err := module.Client().Do(req)
// Assertions
require.NoError(t, err, "Request should not fail")
assert.NotNil(t, resp, "Response should not be nil")
assert.Equal(t, http.StatusOK, resp.StatusCode, "Status code should be 200")
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"), "Content-Type should be application/json")
// Cleanup
if closeErr := resp.Body.Close(); closeErr != nil {
t.Logf("Failed to close response body: %v", closeErr)
}
}
// TestLoggingTransport_GzipBodyNotLogged verifies that when an upstream response uses
// Content-Encoding: gzip, the raw compressed bytes are never written to the logger.
// Regression test for: garbled/unicode log output flooding Datadog when Facade proxies
// large gzip-encoded responses.
func TestLoggingTransport_GzipBodyNotLogged(t *testing.T) {
// Serve a response with Content-Encoding: gzip and a raw gzip magic-byte body.
// In a real proxy the body would be fully compressed; here we just need bytes that
// would appear garbled if logged directly.
gzipMagic := []byte{0x1f, 0x8b, 0x08, 0x00, 0xde, 0xad, 0xbe, 0xef}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Encoding", "gzip")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(gzipMagic)
}))
defer server.Close()
var loggedDetails string
mockLogger := new(MockLogger)
mockLogger.On("Info", mock.Anything, mock.MatchedBy(func(keyvals []interface{}) bool {
for i := 0; i+1 < len(keyvals); i += 2 {
if keyvals[i] == "details" {
loggedDetails = fmt.Sprintf("%v", keyvals[i+1])
}
}
return true
})).Return()
// DisableCompression mirrors the Facade reverse-proxy transport, which must pass
// compressed responses through to callers without auto-decompressing them. Without
// this, Go strips Content-Encoding: gzip from the response before logResponse sees it.
transport := &loggingTransport{
Transport: &http.Transport{DisableCompression: true},
Logger: mockLogger,
LogHeaders: true,
LogBody: true,
}
req, err := http.NewRequestWithContext(context.Background(), "GET", server.URL, nil)
require.NoError(t, err)
resp, err := (&http.Client{Transport: transport}).Do(req)
require.NoError(t, err)
defer resp.Body.Close()
// The details field must contain the omission notice, not raw binary bytes.
assert.Contains(t, loggedDetails, "[body omitted: Content-Encoding=gzip",
"gzip body bytes must not appear in log output")
assert.NotContains(t, loggedDetails, string(gzipMagic),
"raw gzip bytes must not appear in log output")
}
// TestLoggingTransport_GzipFileLogging verifies that the file-logging path
// (handleFileLogging) also omits compressed response bodies, writing the
// omission notice to the transaction log file instead of raw binary bytes.
func TestLoggingTransport_GzipFileLogging(t *testing.T) {
gzipMagic := []byte{0x1f, 0x8b, 0x08, 0x00, 0xde, 0xad, 0xbe, 0xef}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Encoding", "gzip")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(gzipMagic)
}))
defer server.Close()
tmpDir := t.TempDir()
mockLogger := new(MockLogger)
mockLogger.On("Info", mock.Anything, mock.Anything).Return()
mockLogger.On("Error", mock.Anything, mock.Anything).Return()
fileLogger, err := NewFileLogger(tmpDir, mockLogger)
require.NoError(t, err)
transport := &loggingTransport{
Transport: &http.Transport{DisableCompression: true},
Logger: mockLogger,
LogHeaders: true,
LogBody: true,
LogToFile: true,
FileLogger: fileLogger,
}
req, err := http.NewRequestWithContext(context.Background(), "GET", server.URL, nil)
require.NoError(t, err)
resp, err := (&http.Client{Transport: transport}).Do(req)
require.NoError(t, err)
defer resp.Body.Close()
// Read the transaction log file and verify it contains the omission notice.
txnFiles, err := filepath.Glob(filepath.Join(tmpDir, "transactions", "*.log"))
require.NoError(t, err)
require.NotEmpty(t, txnFiles, "expected a transaction log file to be written")
contents, err := os.ReadFile(txnFiles[0])
require.NoError(t, err)
assert.Contains(t, string(contents), "[body omitted: Content-Encoding=gzip",
"transaction log must contain the omission notice")
assert.NotContains(t, string(contents), string(gzipMagic),
"transaction log must not contain raw gzip bytes")
}
// TestConfig_MaxBodyLogSizeDefaultCap verifies that when VerboseOptions are
// explicitly provided with LogBody enabled but MaxBodyLogSize left at 0,
// Validate() applies a safe 1KB default cap.
func TestConfig_MaxBodyLogSizeDefaultCap(t *testing.T) {
t.Run("caps zero to 1024 when LogBody is true", func(t *testing.T) {
cfg := &Config{
Verbose: true,
VerboseOptions: &VerboseOptions{
LogBody: true,
MaxBodyLogSize: 0,
},
}
require.NoError(t, cfg.Validate())
assert.Equal(t, 1024, cfg.VerboseOptions.MaxBodyLogSize)
})
t.Run("preserves explicit non-zero value", func(t *testing.T) {
cfg := &Config{
Verbose: true,
VerboseOptions: &VerboseOptions{
LogBody: true,
MaxBodyLogSize: 5000,
},
}
require.NoError(t, cfg.Validate())
assert.Equal(t, 5000, cfg.VerboseOptions.MaxBodyLogSize)
})
t.Run("does not cap when LogBody is false", func(t *testing.T) {
cfg := &Config{
Verbose: true,
VerboseOptions: &VerboseOptions{
LogBody: false,
MaxBodyLogSize: 0,
},
}
require.NoError(t, cfg.Validate())
assert.Equal(t, 0, cfg.VerboseOptions.MaxBodyLogSize)
})
}
// TestLoggingTransport_UncompressedBodyStillLogged verifies that plain-text
// (non-encoded) responses are still fully logged through both paths, ensuring
// the gzip guard doesn't accidentally suppress normal body logging.
func TestLoggingTransport_UncompressedBodyStillLogged(t *testing.T) {
const bodyText = `{"status":"ok"}`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(bodyText))
}))
defer server.Close()
var loggedDetails string
mockLogger := new(MockLogger)
mockLogger.On("Info", mock.Anything, mock.MatchedBy(func(keyvals []interface{}) bool {
for i := 0; i+1 < len(keyvals); i += 2 {
if keyvals[i] == "details" {
loggedDetails = fmt.Sprintf("%v", keyvals[i+1])
}
}
return true
})).Return()
transport := &loggingTransport{
Transport: &http.Transport{DisableCompression: true},
Logger: mockLogger,
LogHeaders: true,
LogBody: true,
}
req, err := http.NewRequestWithContext(context.Background(), "GET", server.URL, nil)
require.NoError(t, err)
resp, err := (&http.Client{Transport: transport}).Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.True(t, strings.Contains(loggedDetails, bodyText),
"uncompressed response body must appear in log output")
assert.NotContains(t, loggedDetails, "[body omitted:",
"omission notice must not appear for uncompressed responses")
}
// TestLoggingTransport_BinaryContentTypeOmitted verifies that responses with
// binary content types (e.g. image/png, application/octet-stream) have their
// body omitted from logs even when Content-Encoding is not set.
func TestLoggingTransport_BinaryContentTypeOmitted(t *testing.T) {
binaryPayload := []byte{0x89, 0x50, 0x4e, 0x47} // PNG magic bytes
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(binaryPayload)
}))
defer server.Close()
var loggedDetails string
mockLogger := new(MockLogger)
mockLogger.On("Info", mock.Anything, mock.MatchedBy(func(keyvals []interface{}) bool {
for i := 0; i+1 < len(keyvals); i += 2 {
if keyvals[i] == "details" {
loggedDetails = fmt.Sprintf("%v", keyvals[i+1])
}
}
return true
})).Return()
transport := &loggingTransport{
Transport: &http.Transport{DisableCompression: true},
Logger: mockLogger,
LogHeaders: true,
LogBody: true,
}
req, err := http.NewRequestWithContext(context.Background(), "GET", server.URL, nil)
require.NoError(t, err)
resp, err := (&http.Client{Transport: transport}).Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Contains(t, loggedDetails, "[body omitted: Content-Type=image/png",
"binary content type body must be omitted from logs")
assert.NotContains(t, loggedDetails, string(binaryPayload),
"raw binary bytes must not appear in log output")
}
// TestShouldOmitResponseBody verifies the helper correctly classifies content types.
func TestShouldOmitResponseBody(t *testing.T) {
tests := []struct {
name string
contentType string
encoding string
wantOmit bool
}{
{"plain JSON", "application/json", "", false},
{"JSON with charset", "application/json; charset=utf-8", "", false},
{"text/plain", "text/plain", "", false},
{"text/html", "text/html", "", false},
{"vendor+json", "application/vnd.api+json", "", false},
{"vendor+xml", "application/atom+xml", "", false},
{"form urlencoded", "application/x-www-form-urlencoded", "", false},
{"image/png", "image/png", "", true},
{"application/octet-stream", "application/octet-stream", "", true},
{"application/pdf", "application/pdf", "", true},
{"application/protobuf", "application/protobuf", "", true},
{"gzip encoding", "application/json", "gzip", true},
{"br encoding", "text/html", "br", true},
{"no content-type", "", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp := &http.Response{Header: http.Header{}}
if tt.contentType != "" {
resp.Header.Set("Content-Type", tt.contentType)
}
if tt.encoding != "" {
resp.Header.Set("Content-Encoding", tt.encoding)
}
reason := shouldOmitResponseBody(resp)
if tt.wantOmit {
assert.NotEmpty(t, reason, "expected body to be omitted")
} else {
assert.Empty(t, reason, "expected body to be logged")
}
})
}
}