aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/encoding/httpbinding/path_replace_test.go
blob: 689733ca00e877cf70f445b1a1d10e9976fba056 (plain) (blame)
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
package httpbinding

import (
	"bytes"
	"testing"
)

func TestPathReplace(t *testing.T) {
	cases := []struct {
		Orig, ExpPath, ExpRawPath []byte
		Key, Val                  string
	}{
		{
			Orig:       []byte("/{bucket}/{key+}"),
			ExpPath:    []byte("/123/{key+}"),
			ExpRawPath: []byte("/123/{key+}"),
			Key:        "bucket", Val: "123",
		},
		{
			Orig:       []byte("/{bucket}/{key+}"),
			ExpPath:    []byte("/{bucket}/abc"),
			ExpRawPath: []byte("/{bucket}/abc"),
			Key:        "key", Val: "abc",
		},
		{
			Orig:       []byte("/{bucket}/{key+}"),
			ExpPath:    []byte("/{bucket}/a/b/c"),
			ExpRawPath: []byte("/{bucket}/a/b/c"),
			Key:        "key", Val: "a/b/c",
		},
		{
			Orig:       []byte("/{bucket}/{key+}"),
			ExpPath:    []byte("/1/2/3/{key+}"),
			ExpRawPath: []byte("/1%2F2%2F3/{key+}"),
			Key:        "bucket", Val: "1/2/3",
		},
		{
			Orig:       []byte("/{bucket}/{key+}"),
			ExpPath:    []byte("/reallylongvaluegoesheregrowingarray/{key+}"),
			ExpRawPath: []byte("/reallylongvaluegoesheregrowingarray/{key+}"),
			Key:        "bucket", Val: "reallylongvaluegoesheregrowingarray",
		},
	}

	var buffer [64]byte

	for i, c := range cases {
		origRaw := make([]byte, len(c.Orig))
		copy(origRaw, c.Orig)

		path, _, err := replacePathElement(c.Orig, buffer[:0], c.Key, c.Val, false)
		if err != nil {
			t.Fatalf("expected no error, got %v", err)
		}
		rawPath, _, err := replacePathElement(origRaw, buffer[:0], c.Key, c.Val, true)
		if err != nil {
			t.Fatalf("expected no error, got %v", err)
		}

		if e, a := c.ExpPath, path; bytes.Compare(e, a) != 0 {
			t.Errorf("%d, expect uri path to be %q got %q", i, e, a)
		}
		if e, a := c.ExpRawPath, rawPath; bytes.Compare(e, a) != 0 {
			t.Errorf("%d, expect uri raw path to be %q got %q", i, e, a)
		}
	}
}