all repos

viye @ cf8afd92aed2f4da2448c272cbbb65f19b460f5b

my shot at reimplementing xiki

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

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