-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconverter_test.go
More file actions
953 lines (844 loc) · 23.2 KB
/
Copy pathconverter_test.go
File metadata and controls
953 lines (844 loc) · 23.2 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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
package har
import (
"bytes"
"errors"
"io"
"strings"
"testing"
"time"
)
// --- Comprehensive branch coverage tests for converter.go ---
func createTestHarForConvert() *Har {
h := NewHar()
e1 := h.AddEntry("GET", "https://example.com/api/users", "HTTP/1.1", "")
e1.SetResponseStatus(200, "OK")
e1.SetResponseContentText("response body")
e1.Response.Content.MimeType = "application/json"
e1.Response.Content.Size = 100
e1.Time = 150.5
e1.Timings = Timings{
Blocked: 10.0,
DNS: 5.0,
Connect: 20.0,
Send: 1.0,
Wait: 100.0,
Receive: 14.5,
Ssl: 15.0,
}
e2 := h.AddEntry("POST", "https://example.com/api/items", "HTTP/1.1", "")
e2.SetResponseStatus(201, "Created")
e2.SetResponseContentText("created")
e2.Response.Content.MimeType = "text/html"
e2.Response.Content.Size = 50
e2.Time = 200.0
e2.Timings = Timings{
Blocked: 5.0,
DNS: 2.0,
Connect: 10.0,
Send: 2.0,
Wait: 150.0,
Receive: 31.0,
Ssl: 8.0,
}
return h
}
func TestConvertUnsupportedFormat(t *testing.T) {
// Branch: default case in Convert switch
h := createTestHarForConvert()
opts := DefaultConvertOptions()
_, err := h.Convert("xml", opts)
assertHarErrorCode(t, err, ErrCodeUnsupported)
if !strings.Contains(err.Error(), "不支持") && !strings.Contains(err.Error(), "unsupported") {
t.Errorf("Error should mention unsupported format, got: %s", err.Error())
}
}
func TestConvertNilHAR(t *testing.T) {
var h *Har
result, err := h.Convert(FormatCSV, DefaultConvertOptions())
assertHarErrorCode(t, err, ErrCodeInvalidFormat)
if result != "" {
t.Errorf("Expected empty result for nil HAR, got %q", result)
}
}
func TestConvertWithFilter(t *testing.T) {
// Branch: options.Filter != nil
h := createTestHarForConvert()
opts := DefaultConvertOptions()
opts.Filter = &FilterOptions{
Method: "GET",
}
result, err := h.Convert(FormatCSV, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Should only contain the GET request, not the POST
if strings.Contains(result, "POST") {
t.Error("Filtered result should not contain POST method")
}
if !strings.Contains(result, "GET") {
t.Error("Filtered result should contain GET method")
}
}
func TestConvertCSVEmptyEntries(t *testing.T) {
// Branch: convertToCSV with no entries
h := NewHar()
opts := DefaultConvertOptions()
result, err := h.Convert(FormatCSV, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Should have header but no data rows
if !strings.Contains(result, "方法") && !strings.Contains(result, "URL") {
t.Error("CSV should contain header row")
}
// Count lines - should be just header
lines := strings.Count(result, "\n")
if lines < 1 {
t.Error("CSV should have at least a header line")
}
}
func TestConvertCSVWithEntries(t *testing.T) {
// Branch: convertToCSV with entries
h := createTestHarForConvert()
opts := DefaultConvertOptions()
result, err := h.Convert(FormatCSV, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !strings.Contains(result, "GET") {
t.Error("CSV should contain GET method")
}
if !strings.Contains(result, "POST") {
t.Error("CSV should contain POST method")
}
}
func TestConvertMarkdownWithEntries(t *testing.T) {
// Branch: convertToMarkdown with entries
h := createTestHarForConvert()
opts := DefaultConvertOptions()
result, err := h.Convert(FormatMarkdown, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !strings.Contains(result, "|") {
t.Error("Markdown should contain table pipes")
}
if !strings.Contains(result, "---") {
t.Error("Markdown should contain separator row")
}
}
func TestConvertMarkdownEmptyEntries(t *testing.T) {
// Branch: convertToMarkdown with no entries
h := NewHar()
opts := DefaultConvertOptions()
result, err := h.Convert(FormatMarkdown, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !strings.Contains(result, "|") {
t.Error("Markdown should contain header pipes")
}
}
func TestConvertHTMLWithEntries(t *testing.T) {
// Branch: convertToHTML with entries
h := createTestHarForConvert()
opts := DefaultConvertOptions()
result, err := h.Convert(FormatHTML, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !strings.Contains(result, "<table") {
t.Error("HTML should contain table tag")
}
if !strings.Contains(result, "<th>") {
t.Error("HTML should contain header cells")
}
if !strings.Contains(result, "<td>") {
t.Error("HTML should contain data cells")
}
}
func TestConvertHTMLEmptyEntries(t *testing.T) {
// Branch: convertToHTML with no entries
h := NewHar()
opts := DefaultConvertOptions()
result, err := h.Convert(FormatHTML, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !strings.Contains(result, "<table") {
t.Error("HTML should contain table tag")
}
if !strings.Contains(result, "<thead>") {
t.Error("HTML should contain thead")
}
}
func TestConvertTextWithEntries(t *testing.T) {
// Branch: convertToText with entries
h := createTestHarForConvert()
opts := DefaultConvertOptions()
result, err := h.Convert(FormatText, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !strings.Contains(result, "GET") {
t.Error("Text should contain GET method")
}
if !strings.Contains(result, "---") {
t.Error("Text should contain separator line")
}
}
func TestConvertTextEmptyEntries(t *testing.T) {
// Branch: convertToText with no entries
h := NewHar()
opts := DefaultConvertOptions()
result, err := h.Convert(FormatText, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Should have header and separator
if !strings.Contains(result, "---") {
t.Error("Text should contain separator line")
}
}
func TestGetHeadersCustomHeaders(t *testing.T) {
// Branch: len(options.Headers) > 0 returns custom headers
opts := ConvertOptions{
Headers: []string{"Custom1", "Custom2", "Custom3"},
}
headers := getHeaders(opts)
if len(headers) != 3 {
t.Fatalf("Expected 3 custom headers, got %d", len(headers))
}
if headers[0] != "Custom1" {
t.Errorf("Expected 'Custom1', got '%s'", headers[0])
}
if headers[1] != "Custom2" {
t.Errorf("Expected 'Custom2', got '%s'", headers[1])
}
if headers[2] != "Custom3" {
t.Errorf("Expected 'Custom3', got '%s'", headers[2])
}
}
func TestGetHeadersDefaultHeaders(t *testing.T) {
// Branch: default headers from include flags
opts := DefaultConvertOptions()
headers := getHeaders(opts)
if len(headers) == 0 {
t.Fatal("Expected non-empty default headers")
}
// Default options include: URL, Method, Status, ContentType, Size, Time, DateTime
expectedHeaders := []string{"日期时间", "方法", "URL", "状态码", "内容类型", "大小(字节)", "时间(ms)"}
for i, expected := range expectedHeaders {
if i < len(headers) && headers[i] != expected {
t.Errorf("Header[%d]: expected '%s', got '%s'", i, expected, headers[i])
}
}
}
func TestGetHeadersWithTimings(t *testing.T) {
// Branch: IncludeTimings adds timing sub-headers
opts := ConvertOptions{
IncludeTimings: true,
}
headers := getHeaders(opts)
timingHeaders := []string{"阻塞(ms)", "DNS(ms)", "连接(ms)", "发送(ms)", "等待(ms)", "接收(ms)"}
for _, th := range timingHeaders {
found := false
for _, h := range headers {
if h == th {
found = true
break
}
}
if !found {
t.Errorf("Expected timing header '%s' not found in %v", th, headers)
}
}
}
func TestGetHeadersWithPostData(t *testing.T) {
// Branch: IncludePostData adds POST data headers
opts := ConvertOptions{
IncludePostData: true,
}
headers := getHeaders(opts)
postHeaders := []string{"POST数据类型", "POST数据"}
for _, ph := range postHeaders {
found := false
for _, h := range headers {
if h == ph {
found = true
break
}
}
if !found {
t.Errorf("Expected POST header '%s' not found in %v", ph, headers)
}
}
}
func TestGetHeadersWithQueryString(t *testing.T) {
// Branch: IncludeQueryString adds query parameter header
opts := ConvertOptions{
IncludeQueryString: true,
}
headers := getHeaders(opts)
found := false
for _, h := range headers {
if h == "查询参数" {
found = true
break
}
}
if !found {
t.Errorf("Expected '查询参数' header not found in %v", headers)
}
}
func TestGetHeadersWithHeaders(t *testing.T) {
// Branch: IncludeHeaders adds request/response header columns
opts := ConvertOptions{
IncludeHeaders: true,
}
headers := getHeaders(opts)
headerCols := []string{"请求头", "响应头"}
for _, hc := range headerCols {
found := false
for _, h := range headers {
if h == hc {
found = true
break
}
}
if !found {
t.Errorf("Expected header column '%s' not found in %v", hc, headers)
}
}
}
func TestGetHeadersEmptyOptions(t *testing.T) {
// Branch: no include flags set, returns empty headers
opts := ConvertOptions{}
headers := getHeaders(opts)
if len(headers) != 0 {
t.Errorf("Expected empty headers for empty options, got %v", headers)
}
}
func TestCreateDataRowWithPostData(t *testing.T) {
// Branch: IncludePostData with PostData present
entry := Entries{
StartedDateTime: time.Now(),
Request: Request{
Method: "POST",
URL: "https://example.com/api",
PostData: &PostData{
MimeType: "application/json",
Text: `{"key": "value"}`,
},
},
Response: Response{
Status: 201,
StatusText: "Created",
Content: Content{
MimeType: "text/html",
Size: 50,
},
},
Time: 100.0,
}
opts := ConvertOptions{
IncludePostData: true,
}
row := createDataRow(entry, opts)
// Should have mimeType and text from PostData
if len(row) < 2 {
t.Fatalf("Expected at least 2 cells, got %d", len(row))
}
if row[0] != "application/json" {
t.Errorf("Expected 'application/json', got '%s'", row[0])
}
if row[1] != `{"key": "value"}` {
t.Errorf("Expected POST data text, got '%s'", row[1])
}
}
func TestCreateDataRowWithPostDataNil(t *testing.T) {
// Branch: IncludePostData with nil PostData
entry := Entries{
StartedDateTime: time.Now(),
Request: Request{
Method: "GET",
URL: "https://example.com/api",
PostData: nil, // nil PostData
},
Response: Response{
Status: 200,
StatusText: "OK",
Content: Content{
MimeType: "text/html",
Size: 100,
},
},
Time: 50.0,
}
opts := ConvertOptions{
IncludePostData: true,
}
row := createDataRow(entry, opts)
// Should have two empty strings for nil PostData
if len(row) < 2 {
t.Fatalf("Expected at least 2 cells, got %d", len(row))
}
if row[0] != "" {
t.Errorf("Expected empty string for nil PostData mimeType, got '%s'", row[0])
}
if row[1] != "" {
t.Errorf("Expected empty string for nil PostData text, got '%s'", row[1])
}
}
func TestCreateDataRowWithQueryString(t *testing.T) {
// Branch: IncludeQueryString with query params
entry := Entries{
StartedDateTime: time.Now(),
Request: Request{
Method: "GET",
URL: "https://example.com/api?foo=bar&baz=qux",
QueryString: []QueryString{
{Name: "foo", Value: "bar"},
{Name: "baz", Value: "qux"},
},
},
Response: Response{
Status: 200,
StatusText: "OK",
Content: Content{
MimeType: "text/html",
Size: 100,
},
},
Time: 50.0,
}
opts := ConvertOptions{
IncludeQueryString: true,
}
row := createDataRow(entry, opts)
if len(row) < 1 {
t.Fatalf("Expected at least 1 cell, got %d", len(row))
}
qs := row[0]
if !strings.Contains(qs, "foo=bar") {
t.Errorf("Expected query string to contain 'foo=bar', got '%s'", qs)
}
if !strings.Contains(qs, "baz=qux") {
t.Errorf("Expected query string to contain 'baz=qux', got '%s'", qs)
}
}
func TestCreateDataRowWithQueryStringEmpty(t *testing.T) {
// Branch: IncludeQueryString with no query params
entry := Entries{
StartedDateTime: time.Now(),
Request: Request{
Method: "GET",
URL: "https://example.com/api",
QueryString: []QueryString{},
},
Response: Response{
Status: 200,
StatusText: "OK",
Content: Content{
MimeType: "text/html",
Size: 100,
},
},
Time: 50.0,
}
opts := ConvertOptions{
IncludeQueryString: true,
}
row := createDataRow(entry, opts)
if len(row) < 1 {
t.Fatalf("Expected at least 1 cell, got %d", len(row))
}
if row[0] != "" {
t.Errorf("Expected empty string for no query params, got '%s'", row[0])
}
}
func TestCreateDataRowWithHeaders(t *testing.T) {
// Branch: IncludeHeaders with request and response headers
entry := Entries{
StartedDateTime: time.Now(),
Request: Request{
Method: "GET",
URL: "https://example.com/api",
Headers: []Headers{
{Name: "Accept", Value: "application/json"},
{Name: "Authorization", Value: "Bearer token"},
},
},
Response: Response{
Status: 200,
StatusText: "OK",
Headers: []Headers{
{Name: "Content-Type", Value: "application/json"},
{Name: "Cache-Control", Value: "no-cache"},
},
Content: Content{
MimeType: "application/json",
Size: 100,
},
},
Time: 50.0,
}
opts := ConvertOptions{
IncludeHeaders: true,
}
row := createDataRow(entry, opts)
if len(row) < 2 {
t.Fatalf("Expected at least 2 cells for headers, got %d", len(row))
}
// First cell: request headers joined with "; "
if !strings.Contains(row[0], "Accept: application/json") {
t.Errorf("Expected request header 'Accept: application/json' in '%s'", row[0])
}
if !strings.Contains(row[0], "Authorization: Bearer token") {
t.Errorf("Expected request header 'Authorization: Bearer token' in '%s'", row[0])
}
// Second cell: response headers joined with "; "
if !strings.Contains(row[1], "Content-Type: application/json") {
t.Errorf("Expected response header 'Content-Type: application/json' in '%s'", row[1])
}
if !strings.Contains(row[1], "Cache-Control: no-cache") {
t.Errorf("Expected response header 'Cache-Control: no-cache' in '%s'", row[1])
}
}
func TestCreateDataRowWithEmptyHeaders(t *testing.T) {
// Branch: IncludeHeaders with empty header lists
entry := Entries{
StartedDateTime: time.Now(),
Request: Request{
Method: "GET",
URL: "https://example.com/api",
Headers: []Headers{},
},
Response: Response{
Status: 200,
StatusText: "OK",
Headers: []Headers{},
Content: Content{
MimeType: "text/html",
Size: 100,
},
},
Time: 50.0,
}
opts := ConvertOptions{
IncludeHeaders: true,
}
row := createDataRow(entry, opts)
if len(row) < 2 {
t.Fatalf("Expected at least 2 cells for headers, got %d", len(row))
}
if row[0] != "" {
t.Errorf("Expected empty string for no request headers, got '%s'", row[0])
}
if row[1] != "" {
t.Errorf("Expected empty string for no response headers, got '%s'", row[1])
}
}
func TestCreateDataRowWithDateTime(t *testing.T) {
// Branch: IncludeDateTime
now := time.Date(2024, 6, 15, 10, 30, 0, 0, time.UTC)
entry := Entries{
StartedDateTime: now,
Request: Request{
Method: "GET",
URL: "https://example.com/api",
},
Response: Response{
Status: 200,
StatusText: "OK",
Content: Content{
MimeType: "text/html",
Size: 100,
},
},
Time: 50.0,
}
opts := ConvertOptions{
IncludeDateTime: true,
}
row := createDataRow(entry, opts)
if len(row) < 1 {
t.Fatalf("Expected at least 1 cell, got %d", len(row))
}
if !strings.Contains(row[0], "2024-06-15") {
t.Errorf("Expected date in datetime cell, got '%s'", row[0])
}
}
func TestCreateDataRowWithTimings(t *testing.T) {
// Branch: IncludeTimings
entry := Entries{
StartedDateTime: time.Now(),
Request: Request{
Method: "GET",
URL: "https://example.com/api",
},
Response: Response{
Status: 200,
StatusText: "OK",
Content: Content{
MimeType: "text/html",
Size: 100,
},
},
Time: 150.0,
Timings: Timings{
Blocked: 10.0,
DNS: 5.0,
Connect: 20.0,
Send: 1.0,
Wait: 100.0,
Receive: 14.0,
},
}
opts := ConvertOptions{
IncludeTimings: true,
}
row := createDataRow(entry, opts)
if len(row) < 6 {
t.Fatalf("Expected at least 6 timing cells, got %d", len(row))
}
// Check the timing values are formatted as "%.2f"
expectedTimings := []string{"10.00", "5.00", "20.00", "1.00", "100.00", "14.00"}
for i, expected := range expectedTimings {
if row[i] != expected {
t.Errorf("Timing[%d]: expected '%s', got '%s'", i, expected, row[i])
}
}
}
func TestCreateDataRowAllFields(t *testing.T) {
// Branch: all include flags set
now := time.Date(2024, 6, 15, 10, 30, 0, 0, time.UTC)
entry := Entries{
StartedDateTime: now,
Request: Request{
Method: "POST",
URL: "https://example.com/api?foo=bar",
Headers: []Headers{
{Name: "Accept", Value: "application/json"},
},
QueryString: []QueryString{
{Name: "foo", Value: "bar"},
},
PostData: &PostData{
MimeType: "application/json",
Text: `{"key":"value"}`,
},
},
Response: Response{
Status: 200,
StatusText: "OK",
Headers: []Headers{
{Name: "Content-Type", Value: "application/json"},
},
Content: Content{
MimeType: "application/json",
Size: 256,
},
},
Time: 150.0,
Timings: Timings{
Blocked: 10.0,
DNS: 5.0,
Connect: 20.0,
Send: 1.0,
Wait: 100.0,
Receive: 14.0,
},
}
opts := ConvertOptions{
IncludeURL: true,
IncludeMethod: true,
IncludeStatus: true,
IncludeContentType: true,
IncludeSize: true,
IncludeTime: true,
IncludeTimings: true,
IncludeHeaders: true,
IncludeDateTime: true,
IncludePostData: true,
IncludeQueryString: true,
}
row := createDataRow(entry, opts)
// We expect a row with all fields populated
// DateTime, Method, URL, Status, ContentType, Size, Time, 6 timings, PostDataMimeType, PostDataText, QueryString, ReqHeaders, RespHeaders
expectedLen := len(getHeaders(opts))
if len(row) != expectedLen {
t.Errorf("Expected %d cells, got %d", expectedLen, len(row))
}
// Spot check some values
if row[1] != "POST" {
t.Errorf("Expected 'POST' at index 1, got '%s'", row[1])
}
if row[2] != "https://example.com/api?foo=bar" {
t.Errorf("Expected URL at index 2, got '%s'", row[2])
}
if row[3] != "200 OK" {
t.Errorf("Expected '200 OK' at index 3, got '%s'", row[3])
}
}
func TestConvertCSVFullRoundTrip(t *testing.T) {
// End-to-end CSV conversion test
h := createTestHarForConvert()
opts := DefaultConvertOptions()
result, err := h.Convert(FormatCSV, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// CSV should be parseable
if !strings.Contains(result, ",") {
t.Error("CSV output should contain commas")
}
if !strings.Contains(result, "GET") {
t.Error("CSV should contain GET")
}
if !strings.Contains(result, "POST") {
t.Error("CSV should contain POST")
}
}
func TestWriteCSVToWriterWrapsWriterErrors(t *testing.T) {
rootErr := errors.New("csv write failed")
err := writeCSVToWriter(&csvErrorWriter{err: rootErr}, []Entries{{}}, DefaultConvertOptions())
assertHarErrorCode(t, err, ErrCodeFileSystem)
if !errors.Is(err, rootErr) {
t.Fatalf("expected wrapped root error, got %v", err)
}
}
func TestWriteCSVToWriterShortWrite(t *testing.T) {
err := writeCSVToWriter(&shortWriter{}, []Entries{{}}, DefaultConvertOptions())
assertHarErrorCode(t, err, ErrCodeFileSystem)
if !errors.Is(err, io.ErrShortWrite) {
t.Fatalf("expected wrapped short write error, got %v", err)
}
}
func TestWriteCSVToWriterNilWriter(t *testing.T) {
var nilWriter io.Writer
assertHarErrorCode(t, writeCSVToWriter(nilWriter, nil, DefaultConvertOptions()), ErrCodeInvalidFormat)
var typedNilWriter *bytes.Buffer
assertHarErrorCode(t, writeCSVToWriter(typedNilWriter, nil, DefaultConvertOptions()), ErrCodeInvalidFormat)
}
func TestConvertMarkdownWithPipeEscape(t *testing.T) {
// Branch: markdown cell with pipe character should be escaped
h := NewHar()
e := h.AddEntry("GET", "https://example.com/api?a=1|2", "HTTP/1.1", "")
e.SetResponseStatus(200, "OK")
e.Response.Content.MimeType = "text/html"
e.Response.Content.Size = 100
e.Time = 50.0
opts := DefaultConvertOptions()
result, err := h.Convert(FormatMarkdown, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Pipe in URL should be escaped as \|
if !strings.Contains(result, "\\|") {
t.Error("Markdown should escape pipe characters")
}
}
type csvErrorWriter struct {
err error
}
func (w *csvErrorWriter) Write(_ []byte) (int, error) {
return 0, w.err
}
func TestConvertHTMLWithSpecialChars(t *testing.T) {
// Branch: HTML special chars like <, >, & should be escaped
h := NewHar()
e := h.AddEntry("GET", "https://example.com/api?a=<script>&b=1&2", "HTTP/1.1", "")
e.SetResponseStatus(200, "OK")
e.Response.Content.MimeType = "text/html"
e.Response.Content.Size = 100
e.Time = 50.0
opts := DefaultConvertOptions()
result, err := h.Convert(FormatHTML, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// < should be escaped as <
if !strings.Contains(result, "<") {
t.Error("HTML should escape < characters")
}
// & should be escaped as &
if !strings.Contains(result, "&") {
t.Error("HTML should escape & characters")
}
}
func TestConvertAllFormatsWithAllOptions(t *testing.T) {
// Test all formats with all include options enabled
h := NewHar()
e := h.AddEntry("POST", "https://example.com/api?key=val", "HTTP/1.1", "")
e.SetResponseStatus(200, "OK")
e.Response.Content.MimeType = "application/json"
e.Response.Content.Size = 100
e.Time = 150.0
e.AddRequestHeader("Accept", "application/json")
e.AddResponseHeader("Content-Type", "application/json")
e.AddQueryParameter("key", "val")
e.SetPostData("application/json", `{"name":"test"}`)
e.SetTimings(10, 5, 20, 1, 100, 14, 15)
opts := ConvertOptions{
IncludeURL: true,
IncludeMethod: true,
IncludeStatus: true,
IncludeContentType: true,
IncludeSize: true,
IncludeTime: true,
IncludeTimings: true,
IncludeHeaders: true,
IncludeDateTime: true,
IncludePostData: true,
IncludeQueryString: true,
}
formats := []ConvertFormat{FormatCSV, FormatMarkdown, FormatHTML, FormatText}
for _, format := range formats {
t.Run(string(format), func(t *testing.T) {
result, err := h.Convert(format, opts)
if err != nil {
t.Fatalf("Convert %s failed: %v", format, err)
}
if result == "" {
t.Errorf("Convert %s returned empty result", format)
}
})
}
}
func TestConvertFilterResultEmpty(t *testing.T) {
// Branch: Filter matches no entries
h := createTestHarForConvert()
opts := DefaultConvertOptions()
opts.Filter = &FilterOptions{
Method: "DELETE", // No DELETE requests in test data
}
result, err := h.Convert(FormatCSV, opts)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Should have headers but no data rows (only header line)
lines := strings.Split(strings.TrimSpace(result), "\n")
if len(lines) != 1 {
t.Errorf("Expected only header line for empty filter result, got %d lines", len(lines))
}
}
func TestEscapeHTML(t *testing.T) {
// Test escapeHTML function directly
tests := []struct {
input string
expected string
}{
{"<script>", "<script>"},
{"a&b", "a&b"},
{`"quoted"`, ""quoted""},
{"normal text", "normal text"},
{"<a href=\"test\">", "<a href="test">"},
}
for _, tt := range tests {
result := escapeHTML(tt.input)
if result != tt.expected {
t.Errorf("escapeHTML(%q) = %q, expected %q", tt.input, result, tt.expected)
}
}
}