forked from matrix-org/complement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unknown_endpoints_test.go
86 lines (69 loc) · 2.88 KB
/
unknown_endpoints_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package tests
import (
"net/http"
"testing"
"github.com/matrix-org/complement/internal/b"
"github.com/matrix-org/complement/internal/client"
"github.com/matrix-org/complement/internal/match"
"github.com/matrix-org/complement/internal/must"
)
func queryUnknownEndpoint(t *testing.T, user *client.CSAPI, paths []string) {
t.Helper()
res := user.DoFunc(t, "GET", paths)
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: http.StatusNotFound,
JSON: []match.JSON{
match.JSONKeyEqual("errcode", "M_UNRECOGNIZED"),
},
})
}
func queryUnknownMethod(t *testing.T, user *client.CSAPI, method string, paths []string) {
t.Helper()
res := user.DoFunc(t, method, paths)
must.MatchResponse(t, res, match.HTTPResponse{
StatusCode: http.StatusMethodNotAllowed,
JSON: []match.JSON{
match.JSONKeyEqual("errcode", "M_UNRECOGNIZED"),
},
})
}
// Homeservers should return a 404 for unknown endpoints and 405 for incorrect
// methods to known endpoints.
func TestUnknownEndpoints(t *testing.T) {
deployment := Deploy(t, b.BlueprintAlice)
defer deployment.Destroy(t)
alice := deployment.Client(t, "hs1", "@alice:hs1")
// A completely unknown prefix to the matrix project.
t.Run("Unknown prefix", func(t *testing.T) {
queryUnknownEndpoint(t, alice, []string{"_matrix", "unknown"})
})
// Unknown client-server endpoints.
t.Run("Client-server endpoints", func(t *testing.T) {
queryUnknownEndpoint(t, alice, []string{"_matrix", "client", "unknown"})
// v1 should exist, but not v1/unknown.
queryUnknownEndpoint(t, alice, []string{"_matrix", "client", "v1", "unknown"})
queryUnknownEndpoint(t, alice, []string{"_matrix", "client", "v3", "room", "unknown"})
queryUnknownMethod(t, alice, "PUT", []string{"_matrix", "client", "v3", "login"})
})
// Unknown server-server endpoints.
t.Run("Server-server endpoints", func(t *testing.T) {
queryUnknownEndpoint(t, alice, []string{"_matrix", "federation", "unknown"})
// v1 should exist, but not v1/unknown.
queryUnknownEndpoint(t, alice, []string{"_matrix", "federation", "v1", "unknown"})
queryUnknownMethod(t, alice, "PUT", []string{"_matrix", "federation", "v1", "version"})
})
// Unknown key endpoints (part of the Server-server API under a different prefix).
t.Run("Key endpoints", func(t *testing.T) {
queryUnknownEndpoint(t, alice, []string{"_matrix", "key", "unknown"})
// v3 should exist, but not v3/unknown.
queryUnknownEndpoint(t, alice, []string{"_matrix", "key", "v2", "unknown"})
queryUnknownMethod(t, alice, "PUT", []string{"_matrix", "key", "v2", "query"})
})
// Unknown media endpoints.
t.Run("Media endpoints", func(t *testing.T) {
queryUnknownEndpoint(t, alice, []string{"_matrix", "media", "unknown"})
// v3 should exist, but not v3/unknown.
queryUnknownEndpoint(t, alice, []string{"_matrix", "media", "v3", "unknown"})
queryUnknownMethod(t, alice, "PATCH", []string{"_matrix", "media", "v3", "upload"})
})
}