-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathvariables_test.go
More file actions
112 lines (100 loc) · 3.39 KB
/
Copy pathvariables_test.go
File metadata and controls
112 lines (100 loc) · 3.39 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
package render
import (
"strings"
"testing"
api "github.com/bootdotdev/bootdev/client"
)
func TestHTTPVariableSections(t *testing.T) {
result := api.HTTPRequestResult{
Variables: map[string]string{
"authToken": "token-123",
"shortCode": "abc123",
"sessionID": "session-123",
},
Request: api.CLIStepHTTPRequest{
ResponseVariables: []api.HTTPRequestResponseVariable{
{Name: "shortCode", Path: ".short_code"},
{Name: "missingCode", Path: ".missing_code"},
},
ResponseHeaderVariables: []api.HTTPRequestResponseHeaderVariable{
{Name: "sessionID", Header: "Set-Cookie", Regex: "session_id=([^;]+)"},
{Name: "missingSessionID", Header: "Set-Cookie", Regex: "missing=([^;]+)"},
},
Request: api.HTTPRequest{
FullURL: "${baseURL}/api/links/${shortCode}",
Headers: map[string]string{
"Authorization": "Bearer ${authToken}",
},
},
},
}
got := renderVariableSection("Variables Saved", savedVariablesForHTTPResult(result))
got += renderVariableSection("Variables Missing", missingSaveVariablesForHTTPResult(result))
available, expectsVariables := availableVariablesForHTTPResult(result)
if !expectsVariables {
t.Fatalf("expected HTTP request to use variables")
}
got += renderVariableSection("Variables Available", available)
wantContains := []string{
"Variables Saved:",
"sessionID: session-123 (Response Header Set-Cookie matching session_id=([^;]+))",
"shortCode: abc123 (JSON Body .short_code)",
"Variables Missing:",
"missingCode: [not found] (JSON Body .missing_code)",
"missingSessionID: [not found] (Response Header Set-Cookie matching missing=([^;]+))",
"Variables Available:",
"authToken: token-123 (Request Header \"Authorization\")",
"shortCode: abc123 (Request URL)",
}
for _, want := range wantContains {
if !strings.Contains(got, want) {
t.Fatalf("expected %q in:\n%s", want, got)
}
}
if strings.Contains(got, "baseURL") {
t.Fatalf("did not expect special baseURL placeholder in:\n%s", got)
}
}
func TestAvailableVariablesPrintsNotFoundWhenExpectedButUnavailable(t *testing.T) {
result := api.CLICommandResult{
Variables: map[string]string{},
Command: api.CLIStepCLICommand{
Command: "curl ${url}",
},
}
available, expectsVariables := availableVariablesForCLIResult(result)
if !expectsVariables {
t.Fatalf("expected CLI command to use variables")
}
got := renderVariableSection("Variables Available", available)
if !strings.Contains(got, "Variables Available:") {
t.Fatalf("expected Variables Available section in:\n%s", got)
}
if !strings.Contains(got, "url: [not found] (Command)") {
t.Fatalf("expected missing url in:\n%s", got)
}
}
func TestCLIAvailableVariables(t *testing.T) {
result := api.CLICommandResult{
Variables: map[string]string{
"url": "http://localhost:42069",
},
Command: api.CLIStepCLICommand{
Command: "curl ${url}",
Tests: []api.CLICommandTest{
{StdoutContainsAll: []string{"${expected}"}},
},
},
}
available, expectsVariables := availableVariablesForCLIResult(result)
if !expectsVariables {
t.Fatalf("expected CLI command to use variables")
}
got := renderVariableSection("Variables Available", available)
if !strings.Contains(got, "url: http://localhost:42069 (Command)") {
t.Fatalf("expected url entry in:\n%s", got)
}
if !strings.Contains(got, "expected: [not found] (Stdout Contains Test)") {
t.Fatalf("expected missing expected entry in:\n%s", got)
}
}