|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestHeader(t *testing.T) { |
| 11 | + // Manually create a response object with headers |
| 12 | + resp := &http.Response{ |
| 13 | + Header: http.Header{ |
| 14 | + "Access-Control-Allow-Headers": []string{"Content-Type, Range, User-Agent, X-Requested-With"}, |
| 15 | + }, |
| 16 | + } |
| 17 | + |
| 18 | + // Extract headers from the response |
| 19 | + headers := resp.Header["Access-Control-Allow-Headers"] |
| 20 | + |
| 21 | + // Test the HeaderBuilder function |
| 22 | + hb := Header("Access-Control-Allow-Headers").Has("Content-Type", "Range", "User-Agent", "X-Requested-With") |
| 23 | + |
| 24 | + checkOutput := hb.Check_.Check(headers) |
| 25 | + |
| 26 | + // Check if the headers satisfy the conditions |
| 27 | + assert.True(t, checkOutput.Success, checkOutput.Reason) |
| 28 | +} |
| 29 | + |
| 30 | +func TestHeaderFailure(t *testing.T) { |
| 31 | + resp := &http.Response{ |
| 32 | + Header: http.Header{ |
| 33 | + // missing X-Requested-With |
| 34 | + "Access-Control-Allow-Headers": []string{"Content-Type, Range, User-Agent"}, |
| 35 | + }, |
| 36 | + } |
| 37 | + // Extract headers from the response |
| 38 | + headers := resp.Header["Access-Control-Allow-Headers"] |
| 39 | + |
| 40 | + // Test the HeaderBuilder function |
| 41 | + hb := Header("Access-Control-Allow-Headers").Has("Content-Type", "Range", "User-Agent", "X-Requested-With") |
| 42 | + |
| 43 | + checkOutput := hb.Check_.Check(headers) |
| 44 | + |
| 45 | + assert.False(t, checkOutput.Success, checkOutput.Reason) |
| 46 | +} |
0 commit comments