File: response_test.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (108 lines) | stat: -rw-r--r-- 2,467 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 2021, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

package internal_test

import (
	"bytes"
	"io"
	"net/http"
	"testing"

	"github.com/maxatome/go-testdeep/helpers/tdhttp/internal"
	"github.com/maxatome/go-testdeep/internal/test"
	"github.com/maxatome/go-testdeep/td"
)

func newResponse(body string) *http.Response {
	return &http.Response{
		Status:     "200 OK",
		StatusCode: 200,
		Proto:      "HTTP/1.0",
		ProtoMajor: 1,
		ProtoMinor: 0,
		Header: http.Header{
			"A": []string{"foo"},
			"B": []string{"bar"},
		},
		Body: io.NopCloser(bytes.NewBufferString(body)),
	}
}

func inBQ(s string) string {
	return "`" + s + "`"
}

func TestDumpResponse(t *testing.T) {
	tb := test.NewTestingTB("TestDumpResponse")
	internal.DumpResponse(tb, newResponse("one-line"))
	td.Cmp(t, tb.LastMessage(),
		`Received response:
`+inBQ(`HTTP/1.0 200 OK
A: foo
B: bar

one-line`))

	tb.ResetMessages()
	internal.DumpResponse(tb, newResponse("multi\r\nlines\r\nand\ttabs héhé"))
	td.Cmp(t, tb.LastMessage(),
		`Received response:
`+inBQ(`HTTP/1.0 200 OK
A: foo
B: bar

multi
lines
`+"and\ttabs héhé"))

	tb.ResetMessages()
	internal.DumpResponse(tb, newResponse("multi\nlines\nand\ttabs héhé"))
	td.Cmp(t, tb.LastMessage(),
		`Received response:
`+inBQ(`HTTP/1.0 200 OK
A: foo
B: bar

multi
lines
`+"and\ttabs héhé"))

	// one \r more in body
	tb.ResetMessages()
	internal.DumpResponse(tb, newResponse("multi\r\nline\r"))
	td.Cmp(t, tb.LastMessage(),
		`Received response:
"HTTP/1.0 200 OK\r\nA: foo\r\nB: bar\r\n\r\nmulti\r\nline\r"`)

	// BOM
	tb.ResetMessages()
	internal.DumpResponse(tb, newResponse("\ufeff"))
	td.Cmp(t, tb.LastMessage(),
		`Received response:
"HTTP/1.0 200 OK\r\nA: foo\r\nB: bar\r\n\r\n\ufeff"`)

	// Rune error
	tb.ResetMessages()
	internal.DumpResponse(tb, newResponse("\xf4\x9f\xbf\xbf"))
	td.Cmp(t, tb.LastMessage(),
		`Received response:
"HTTP/1.0 200 OK\r\nA: foo\r\nB: bar\r\n\r\n\xf4\x9f\xbf\xbf"`)

	// `
	tb.ResetMessages()
	internal.DumpResponse(tb, newResponse("he`o"))
	td.Cmp(t, tb.LastMessage(),
		`Received response:
"HTTP/1.0 200 OK\r\nA: foo\r\nB: bar\r\n\r\nhe`+"`"+`o"`)

	// 0x7f
	tb.ResetMessages()
	internal.DumpResponse(tb, newResponse("\x7f"))
	td.Cmp(t, tb.LastMessage(),
		td.Re(`Received response:
"HTTP/1.0 200 OK\\r\\nA: foo\\r\\nB: bar\\r\\n\\r\\n(\\u007f|\\x7f)"`))
}