summaryrefslogtreecommitdiff
path: root/pkg/prxy/prxy_test.go
blob: 6f37974e749393f0d99a08dbc7b22669ad7b20d8 (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
package prxy

import (
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/xlgmokha/x/pkg/x"
	"gitlab.com/mokhax/spike/pkg/test"
)

func TestProxy(t *testing.T) {
	t.Run("http://idp.test", func(t *testing.T) {
		var lastIdPRequest *http.Request
		var lastUiRequest *http.Request

		idp := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			lastIdPRequest = r
			w.WriteHeader(http.StatusOK)
		}))
		defer idp.Close()

		ui := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			lastUiRequest = r
			w.WriteHeader(http.StatusTeapot)
		}))
		defer ui.Close()

		subject := New(map[string]string{
			"idp.test": idp.URL,
			"ui.test":  ui.URL,
		})

		r, w := test.RequestResponse("GET", "http://idp.test:8080/saml/new")

		subject.ServeHTTP(w, r)

		url := x.Must(url.Parse(idp.URL))

		assert.Nil(t, lastUiRequest)
		assert.Equal(t, http.StatusOK, w.Code)

		require.NotNil(t, lastIdPRequest)
		assert.Equal(t, url.Host, lastIdPRequest.Host)
	})
}