all repos

viye @ f73a9495284789560e2bc18286979763ae4d5c39

my shot at reimplementing xiki

viye/internal/plumbing/http/http_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
update some inconsistencies, 1 month ago
1
package http
2
3
import (
4
	"io"
5
	"net/http"
6
	"net/http/httptest"
7
	"testing"
8
9
	"github.com/olexsmir/viye/internal/viye"
10
11
	"olexsmir.xyz/x/is"
12
)
13
14
func TestMatch(t *testing.T) {
15
	tests := map[string]bool{
16
		"get":    true,
17
		"post":   true,
18
		"put":    true,
19
		"patch":  true,
20
		"delete": true,
21
		"empty":  false,
22
		"":       false,
23
	}
24
	for tinp, twant := range tests {
25
		got := (Tool{}).Match(&viye.Context{Cmd: tinp})
26
		is.Equal(t, twant, got)
27
	}
28
}
29
30
func TestExecute(t *testing.T) {
31
	t.Run("no args returns error", func(t *testing.T) {
32
		_, err := (Tool{}).Execute(&viye.Context{Cmd: "get", Args: []string{}})
33
		is.Err(t, err, "provide an url")
34
	})
35
36
	t.Run("invalid url returns error", func(t *testing.T) {
37
		_, err := (Tool{}).Execute(&viye.Context{Cmd: "get", Args: []string{"not-a-url"}})
38
		is.Err(t, err, "provide an url")
39
	})
40
41
	t.Run("successful GET request", func(t *testing.T) {
42
		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
43
			is.Equal(t, "GET", r.Method)
44
			w.WriteHeader(http.StatusOK)
45
			io.WriteString(w, "response body")
46
		}))
47
		defer server.Close()
48
49
		got, err := (Tool{}).Execute(&viye.Context{
50
			Cmd:  "get",
51
			Args: []string{server.URL},
52
		})
53
		is.Err(t, err, nil)
54
		is.Equal(t, "| 200\tresponse body\n", got)
55
	})
56
57
	t.Run("POST request with body", func(t *testing.T) {
58
		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
59
			is.Equal(t, "POST", r.Method)
60
			body, _ := io.ReadAll(r.Body)
61
			is.Equal(t, "test body", string(body))
62
			w.WriteHeader(http.StatusCreated)
63
			io.WriteString(w, "created")
64
		}))
65
		defer server.Close()
66
67
		got, err := (Tool{}).Execute(&viye.Context{
68
			Cmd:  "post",
69
			Args: []string{server.URL},
70
			Body: []string{"test body"},
71
		})
72
		is.Err(t, err, nil)
73
		is.Equal(t, "| 201\tcreated\n", got)
74
	})
75
76
	t.Run("request with headers", func(t *testing.T) {
77
		server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
78
			is.Equal(t, "application/json", r.Header.Get("Accept"))
79
			w.WriteHeader(http.StatusOK)
80
		}))
81
		defer server.Close()
82
83
		_, err := (Tool{}).Execute(&viye.Context{
84
			Cmd:  "get",
85
			Args: []string{server.URL},
86
			Body: []string{"Accept: application/json"},
87
		})
88
		is.Err(t, err, nil)
89
	})
90
91
	t.Run("nonexistent server returns error", func(t *testing.T) {
92
		_, err := (Tool{}).Execute(&viye.Context{
93
			Cmd:  "get",
94
			Args: []string{"http://localhost:1"},
95
		})
96
		is.Err(t, err, "failed to make a request:")
97
	})
98
}
99
100
func TestParseBody(t *testing.T) {
101
	tests := map[string]struct {
102
		inp         []string
103
		wantHeaders map[string]string
104
		wantBody    string
105
	}{
106
		"empty input": {
107
			inp:         []string{},
108
			wantHeaders: nil,
109
			wantBody:    "",
110
		},
111
		"headers only": {
112
			inp: []string{"Header: Value", "Content-Type: no"},
113
			wantHeaders: map[string]string{
114
				"Header":       "Value",
115
				"Content-Type": "no",
116
			},
117
		},
118
		"headers with body": {
119
			inp:      []string{"Header: Value", "Content-Type: no", "some input"},
120
			wantBody: "some input",
121
			wantHeaders: map[string]string{
122
				"Header":       "Value",
123
				"Content-Type": "no",
124
			},
125
		},
126
		"body with colon in value": {
127
			inp:         []string{"Accept: application/json", "plain text body"},
128
			wantHeaders: map[string]string{"Accept": "application/json"},
129
			wantBody:    "plain text body",
130
		},
131
	}
132
	for tname, tt := range tests {
133
		t.Run(tname, func(t *testing.T) {
134
			headers, body, err := parseBody(tt.inp)
135
			is.Err(t, err, nil)
136
			is.Equal(t, tt.wantHeaders, headers)
137
			is.Equal(t, tt.wantBody, body)
138
		})
139
	}
140
}