|
1 | 1 | package gateway |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "net/http" |
5 | 6 | "net/http/httptest" |
6 | 7 | "net/url" |
7 | 8 | "testing" |
8 | 9 | "time" |
9 | 10 |
|
10 | 11 | "github.com/TykTechnologies/tyk/apidef" |
| 12 | + "github.com/TykTechnologies/tyk/config" |
| 13 | + "github.com/TykTechnologies/tyk/header" |
11 | 14 |
|
12 | 15 | "github.com/stretchr/testify/assert" |
13 | 16 |
|
@@ -75,6 +78,82 @@ func TestRateLimitForAPI_EnabledForSpec(t *testing.T) { |
75 | 78 | assert.False(t, rlDisabled.EnabledForSpec()) |
76 | 79 | } |
77 | 80 |
|
| 81 | +func TestAPIRateLimitResponseHeaders(t *testing.T) { |
| 82 | + limiters := []string{"Redis", "Sentinel", "DRL", "FixedWindow"} |
| 83 | + |
| 84 | + for _, limiter := range limiters { |
| 85 | + t.Run("API Rate limit headers for "+limiter, func(t *testing.T) { |
| 86 | + ts := StartTest(func(globalConf *config.Config) { |
| 87 | + globalConf.RateLimitResponseHeaders = config.SourceRateLimits |
| 88 | + |
| 89 | + switch limiter { |
| 90 | + case "Redis": |
| 91 | + globalConf.EnableRedisRollingLimiter = true |
| 92 | + case "Sentinel": |
| 93 | + globalConf.EnableSentinelRateLimiter = true |
| 94 | + case "DRL": |
| 95 | + globalConf.DRLEnableSentinelRateLimiter = true |
| 96 | + case "FixedWindow": |
| 97 | + globalConf.EnableFixedWindowRateLimiter = true |
| 98 | + } |
| 99 | + }) |
| 100 | + defer ts.Close() |
| 101 | + |
| 102 | + var ( |
| 103 | + rateLimitRate float64 = 2 |
| 104 | + rateLimitPer float64 = 10 |
| 105 | + ) |
| 106 | + |
| 107 | + _ = ts.Gw.BuildAndLoadAPI(func(spec *APISpec) { |
| 108 | + spec.APIID = "api-rate-limit-headers-test-" + limiter |
| 109 | + spec.Proxy.ListenPath = "/api-rate-limit-headers-test" |
| 110 | + spec.UseKeylessAccess = true |
| 111 | + spec.GlobalRateLimit = apidef.GlobalRateLimit{ |
| 112 | + Disabled: false, |
| 113 | + Rate: rateLimitRate, |
| 114 | + Per: rateLimitPer, |
| 115 | + } |
| 116 | + })[0] |
| 117 | + |
| 118 | + expectedRemaining1 := fmt.Sprintf("%d", int(rateLimitRate)-1) |
| 119 | + expectedRemaining2 := fmt.Sprintf("%d", int(rateLimitRate)-2) |
| 120 | + |
| 121 | + headersMatch1 := map[string]string{ |
| 122 | + header.XRateLimitLimit: fmt.Sprintf("%d", int(rateLimitRate)), |
| 123 | + } |
| 124 | + headersMatch2 := map[string]string{ |
| 125 | + header.XRateLimitLimit: fmt.Sprintf("%d", int(rateLimitRate)), |
| 126 | + } |
| 127 | + |
| 128 | + // For limiters that don't support Remaining (Sentinel, FixedWindow), it should be assigned to 0. |
| 129 | + if limiter == "Redis" || limiter == "DRL" { |
| 130 | + headersMatch1[header.XRateLimitRemaining] = expectedRemaining1 |
| 131 | + headersMatch2[header.XRateLimitRemaining] = expectedRemaining2 |
| 132 | + } else { |
| 133 | + headersMatch1[header.XRateLimitRemaining] = "0" |
| 134 | + headersMatch2[header.XRateLimitRemaining] = "0" |
| 135 | + } |
| 136 | + |
| 137 | + _, _ = ts.Run(t, []test.TestCase{ |
| 138 | + { |
| 139 | + Path: "/api-rate-limit-headers-test", |
| 140 | + Code: http.StatusOK, |
| 141 | + HeadersMatch: headersMatch1, |
| 142 | + }, |
| 143 | + { |
| 144 | + Path: "/api-rate-limit-headers-test", |
| 145 | + Code: http.StatusOK, |
| 146 | + HeadersMatch: headersMatch2, |
| 147 | + }, |
| 148 | + { |
| 149 | + Path: "/api-rate-limit-headers-test", |
| 150 | + Code: http.StatusTooManyRequests, |
| 151 | + }, |
| 152 | + }...) |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
| 156 | + |
78 | 157 | func TestRLOpen(t *testing.T) { |
79 | 158 | ts := StartTest(nil) |
80 | 159 | defer ts.Close() |
|
0 commit comments