-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
version_test.go
68 lines (52 loc) · 2.16 KB
/
version_test.go
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
package versioning_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/kataras/versioning"
)
func TestGetVersion(t *testing.T) {
router := http.NewServeMux()
writeVesion := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(versioning.GetVersion(r)))
})
router.Handle("/", writeVesion)
router.HandleFunc("/manual", func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(versioning.WithVersion(context.Background(), "11.0.5"))
writeVesion.ServeHTTP(w, r)
})
srv := httptest.NewServer(router)
defer srv.Close()
expect(t, http.MethodGet, srv.URL, withHeader(versioning.AcceptVersionHeaderKey, "1.0")).
statusCode(http.StatusOK).
bodyEq("1.0")
expect(t, http.MethodGet, srv.URL, withHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version=2.1")).
statusCode(http.StatusOK).
bodyEq("2.1")
expect(t, http.MethodGet, srv.URL, withHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version=2.1 ;other=dsa")).
statusCode(http.StatusOK).
bodyEq("2.1")
expect(t, http.MethodGet, srv.URL, withHeader(versioning.AcceptHeaderKey, "version=2.1")).
statusCode(http.StatusOK).
bodyEq("2.1")
expect(t, http.MethodGet, srv.URL, withHeader(versioning.AcceptHeaderKey, "version=1")).
statusCode(http.StatusOK).
bodyEq("1")
// unknown versions.
expect(t, http.MethodGet, srv.URL, withHeader(versioning.AcceptVersionHeaderKey, "")).
statusCode(http.StatusOK).
bodyEq(versioning.NotFound)
expect(t, http.MethodGet, srv.URL, withHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version=")).
statusCode(http.StatusOK).
bodyEq(versioning.NotFound)
expect(t, http.MethodGet, srv.URL, withHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version= ;other=dsa")).
statusCode(http.StatusOK).
bodyEq(versioning.NotFound)
expect(t, http.MethodGet, srv.URL, withHeader(versioning.AcceptHeaderKey, "version=")).
statusCode(http.StatusOK).
bodyEq(versioning.NotFound)
expect(t, http.MethodGet, srv.URL+"/manual", withHeader(versioning.AcceptHeaderKey, "application/vnd.api+json; version= ;other=dsa")).
statusCode(http.StatusOK).
bodyEq("11.0.5")
}