This repository has no description
1.6 kB
38 lines
1package xrpc
2
3import (
4 "net/http/httptest"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8)
9
10func TestForwardedForAppendsThePeerToTheChain(t *testing.T) {
11 cases := []struct {
12 name string
13 remote string
14 chain []string
15 want string
16 }{
17 {"a direct caller is the whole chain", "203.0.113.7:52344", nil, "203.0.113.7"},
18 {"a portless remote address passes through", "203.0.113.7", nil, "203.0.113.7"},
19 {"bobbin's client address stays left of bobbin", "198.51.100.4:41000", []string{"203.0.113.7"}, "203.0.113.7, 198.51.100.4"},
20 {"a chain split across header lines joins into a single value", "198.51.100.4:41000", []string{"203.0.113.7", "192.0.2.9"}, "203.0.113.7, 192.0.2.9, 198.51.100.4"},
21 {"a blank entry never leaves a gap the knot has to skip", "198.51.100.4:41000", []string{"", " ", "203.0.113.7"}, "203.0.113.7, 198.51.100.4"},
22 {"an ipv6 peer loses its port and keeps its colons", "[2001:db8::5]:41000", []string{"203.0.113.7"}, "203.0.113.7, 2001:db8::5"},
23 {"a forged entry stays left of the address that sent it", "203.0.113.7:52344", []string{"192.0.2.9"}, "192.0.2.9, 203.0.113.7"},
24 }
25
26 for _, c := range cases {
27 t.Run(c.name, func(t *testing.T) {
28 r := httptest.NewRequest("GET", "/xrpc/sh.tangled.git.temp.getTree?repo=did:plc:limpet", nil)
29 r.RemoteAddr = c.remote
30 for _, entry := range c.chain {
31 r.Header.Add(forwardedForHeader, entry)
32 }
33
34 assert.Equal(t, c.want, forwardedFor(r))
35 assert.Equal(t, c.chain, r.Header.Values(forwardedForHeader), "the caller's own header must survive the read")
36 })
37 }
38}