summaryrefslogtreecommitdiff
path: root/pkg/test/http_test.go
blob: ae2e8208bcc2966680a144b332e56b5fd6b89499 (plain)
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
package test

import (
	"fmt"
	"io"
	"net/http"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/xlgmokha/x/pkg/context"
	"github.com/xlgmokha/x/pkg/serde"
	"github.com/xlgmokha/x/pkg/x"
)

var exampleHeader x.Option[*http.Request] = x.Option[*http.Request](func(r *http.Request) *http.Request {
	r.Header.Add("X-Example", "example")
	return r
})

var withHost x.Option[*http.Request] = x.Option[*http.Request](func(r *http.Request) *http.Request {
	r.Host = "example.com"
	return r
})

func TestRequest(t *testing.T) {
	t.Run("without options", func(t *testing.T) {
		r := Request("GET", "/example")

		require.NotNil(t, r)
		assert.Equal(t, "GET", r.Method)
		assert.Equal(t, "/example", r.URL.Path)
		assert.Zero(t, r.Body)
	})

	t.Run("with an option", func(t *testing.T) {
		r := Request("GET", "/example", exampleHeader)

		require.NotNil(t, r)
		assert.Equal(t, "GET", r.Method)
		assert.Equal(t, "/example", r.URL.Path)
		assert.Zero(t, r.Body)
		assert.Equal(t, "example", r.Header.Get("X-Example"))
	})

	t.Run("with options", func(t *testing.T) {
		r := Request("GET", "/example", exampleHeader, withHost)

		require.NotNil(t, r)
		assert.Equal(t, "GET", r.Method)
		assert.Equal(t, "/example", r.URL.Path)
		assert.Zero(t, r.Body)
		assert.Equal(t, "example", r.Header.Get("X-Example"))
		assert.Equal(t, "example.com", r.Host)
	})
}

func TestRequestResponse(t *testing.T) {
	t.Run("without options", func(t *testing.T) {
		r, w := RequestResponse("GET", "/health")

		require.NotNil(t, r)
		assert.Equal(t, "GET", r.Method)
		assert.Equal(t, "/health", r.URL.Path)

		require.NotNil(t, w)
	})

	t.Run("with options", func(t *testing.T) {
		r, w := RequestResponse("GET", "/example", exampleHeader, withHost)

		require.NotNil(t, r)
		assert.Equal(t, "GET", r.Method)
		assert.Equal(t, "/example", r.URL.Path)
		assert.Zero(t, r.Body)
		assert.Equal(t, "example", r.Header.Get("X-Example"))
		assert.Equal(t, "example.com", r.Host)

		require.NotNil(t, w)
	})
}

func TestWithAcceptHeader(t *testing.T) {
	t.Run("applies the Accept header", func(t *testing.T) {
		r := Request("GET", "/example", WithAcceptHeader(serde.JSON))

		require.NotNil(t, r)
		assert.Equal(t, serde.JSON.String(), r.Header.Get("Accept"))
	})
}

func TestWithRequestHeader(t *testing.T) {
	t.Run("applies a header to the request", func(t *testing.T) {
		r := Request("GET", "/example", WithRequestHeader("Via", "gtwy"))

		require.NotNil(t, r)
		assert.Equal(t, "gtwy", r.Header.Get("Via"))
	})
}

func TestWithContentType(t *testing.T) {
	type example struct {
		ID   int    `json: "id" jsonapi:"primary,examples"`
		Name string `json: "name" jsonapi:"attr,name"`
	}
	item := &example{ID: 1, Name: "example"}

	tt := []serde.MediaType{serde.JSON, serde.YAML}
	for _, mediaType := range tt {
		t.Run(fmt.Sprintf("generates a %v request body", mediaType), func(t *testing.T) {
			r := Request("GET", "/example", WithContentType(item, mediaType))
			require.NotNil(t, r)

			result, err := serde.From[example](r.Body, mediaType)
			require.NoError(t, err)
			assert.Equal(t, 1, result.ID)
			assert.Equal(t, "example", result.Name)
		})
	}
}

func TestWithRequestBody(t *testing.T) {
	t.Run("applies the io to the request body", func(t *testing.T) {
		body := io.NopCloser(strings.NewReader("example"))
		r := Request("GET", "/example", WithRequestBody(body))

		require.NotNil(t, r)

		b, err := io.ReadAll(r.Body)
		require.NoError(t, err)
		assert.Equal(t, "example", string(b))
	})
}

func TestWithContext(t *testing.T) {
	t.Run("returns a request with a new context", func(t *testing.T) {
		key := context.Key[string]("x")

		ctx := key.With(t.Context(), "example")
		r := Request("GET", "/example", WithContext(ctx))

		require.NotNil(t, r)
		assert.Equal(t, "example", key.From(r.Context()))
	})
}

func TestWithContextKeyValue(t *testing.T) {
	t.Run("returns a request with a new context", func(t *testing.T) {
		key := context.Key[string]("x")

		r := Request("GET", "/example", WithContextKeyValue(t.Context(), key, "example"))

		require.NotNil(t, r)
		assert.Equal(t, "example", key.From(r.Context()))
	})
}

func TestWithCookie(t *testing.T) {
	t.Run("adds a cookie to the request", func(t *testing.T) {
		r := Request("GET", "/example", WithCookie(&http.Cookie{Name: "example", Value: "value"}))

		require.NotNil(t, r)
		result, err := r.Cookie("example")
		require.NoError(t, err)

		assert.Equal(t, "example", result.Name)
		assert.Equal(t, "value", result.Value)
	})
}