MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestPublicRESTStatCount

Function TestPublicRESTStatCount

rest/api_test.go:83–141  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

81}
82
83func TestPublicRESTStatCount(t *testing.T) {
84 rt := NewRestTester(t, &RestTesterConfig{SyncFn: channels.DocChannelsSyncFunction})
85 defer rt.Close()
86
87 // create a user to authenticate as for public api calls and assert the stat hasn't incremented as a result
88 rt.CreateUser("greg", []string{"ABC"})
89 base.RequireWaitForStat(t, func() int64 {
90 return rt.GetDatabase().DbStats.DatabaseStats.NumPublicRestRequests.Value()
91 }, 0)
92
93 // use public api to put a doc through SGW then assert the stat has increased by 1
94 resp := rt.SendUserRequest(http.MethodPut, "/{{.keyspace}}/doc1", `{"foo":"bar", "channels":["ABC"]}`, "greg")
95 RequireStatus(t, resp, http.StatusCreated)
96 base.RequireWaitForStat(t, func() int64 {
97 return rt.GetDatabase().DbStats.DatabaseStats.NumPublicRestRequests.Value()
98 }, 1)
99
100 // send admin request assert that the public rest count doesn't increase
101 resp = rt.SendAdminRequest(http.MethodGet, "/{{.keyspace}}/doc1", "")
102 RequireStatus(t, resp, http.StatusOK)
103 base.RequireWaitForStat(t, func() int64 {
104 return rt.GetDatabase().DbStats.DatabaseStats.NumPublicRestRequests.Value()
105 }, 1)
106
107 // send another public request to assert the stat increases by 1
108 resp = rt.SendUserRequest(http.MethodGet, "/{{.keyspace}}/doc1", "", "greg")
109 RequireStatus(t, resp, http.StatusOK)
110 base.RequireWaitForStat(t, func() int64 {
111 return rt.GetDatabase().DbStats.DatabaseStats.NumPublicRestRequests.Value()
112 }, 2)
113
114 resp = rt.SendUserRequest(http.MethodGet, "/{{.db}}/_blipsync", "", "greg")
115 RequireStatus(t, resp, http.StatusUpgradeRequired)
116
117 base.RequireWaitForStat(t, func() int64 {
118 return rt.GetDatabase().DbStats.DatabaseStats.NumPublicRestRequests.Value()
119 }, 2)
120
121 srv := httptest.NewServer(rt.TestMetricsHandler())
122 defer srv.Close()
123
124 // test metrics endpoint
125 response, err := http.Get(srv.URL + "/_metrics")
126 require.NoError(t, err)
127 require.NoError(t, response.Body.Close())
128 assert.Equal(t, http.StatusOK, response.StatusCode)
129 // assert the stat doesn't increment
130 base.RequireWaitForStat(t, func() int64 {
131 return rt.GetDatabase().DbStats.DatabaseStats.NumPublicRestRequests.Value()
132 }, 2)
133
134 // test public endpoint but one that doesn't access a db
135 resp = rt.SendUserRequest(http.MethodGet, "/", "", "greg")
136 RequireStatus(t, resp, http.StatusOK)
137 // assert the stat doesn't increment
138 base.RequireWaitForStat(t, func() int64 {
139 return rt.GetDatabase().DbStats.DatabaseStats.NumPublicRestRequests.Value()
140 }, 2)

Callers

nothing calls this directly

Calls 13

CloseMethod · 0.95
CreateUserMethod · 0.95
GetDatabaseMethod · 0.95
SendUserRequestMethod · 0.95
SendAdminRequestMethod · 0.95
TestMetricsHandlerMethod · 0.95
RequireWaitForStatFunction · 0.92
NewRestTesterFunction · 0.85
RequireStatusFunction · 0.85
CloseMethod · 0.65
ValueMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected