code.vegaprotocol.io/vega@v0.79.0/datanode/integration/ratelimit_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package integration_test 17 18 import ( 19 "net/http" 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestRateLimit(t *testing.T) { 27 url := "http://localhost:3008/api/v2/info" 28 for { 29 // keep making requests against the http API until we get a response that is not 200 30 // this response should be 429 31 resp, err := http.Get(url) 32 require.NoError(t, err) 33 if resp.StatusCode != http.StatusOK { 34 assert.Equal(t, http.StatusTooManyRequests, resp.StatusCode) 35 break 36 } 37 assert.Len(t, resp.Header.Get("RateLimit-Retry-After"), 0) 38 _ = resp.Body.Close() 39 } 40 41 for { 42 // continue making requests against the http API until we get a response that is not 429 or 200 43 // this response should be 403 (Forbidden) 44 // we have to check for 200 because in the time it takes to make the requests and get a response 45 // our token bucket may have refilled allowing us to make more requests 46 resp, err := http.Get(url) 47 require.NoError(t, err) 48 if resp.StatusCode != http.StatusTooManyRequests && resp.StatusCode != http.StatusOK { 49 assert.Equal(t, http.StatusForbidden, resp.StatusCode) 50 assert.Len(t, resp.Header.Get("RateLimit-Retry-After"), 3) 51 break 52 } 53 assert.Len(t, resp.Header.Get("RateLimit-Retry-After"), 0) 54 _ = resp.Body.Close() 55 } 56 }