mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-05 09:14:28 +00:00
4c47756aed
* remove validation package * structs cleanup * merge with apimiddleware removal * more validation and Bls capitalization * builder test fix * use strconv for uint->str conversions * use DecodeHexWithLength * use exact param names * rename http package to httputil * change conversions to fmt.Sprintf * handle query paramsd and route variables * spans and receiver name * split structs, move bytes helper * missing ok check * fix reference to indexed failure * errors fixup * add godoc to helper * fix BLS casing and chainhead ref * review * fix import in tests * gzl
89 lines
3.0 KiB
Go
89 lines
3.0 KiB
Go
package httputil
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/api"
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
)
|
|
|
|
func TestSSZRequested(t *testing.T) {
|
|
t.Run("ssz_requested", func(t *testing.T) {
|
|
request := httptest.NewRequest("GET", "http://foo.example", nil)
|
|
request.Header["Accept"] = []string{api.OctetStreamMediaType}
|
|
result := SszRequested(request)
|
|
assert.Equal(t, true, result)
|
|
})
|
|
|
|
t.Run("ssz_content_type_first", func(t *testing.T) {
|
|
request := httptest.NewRequest("GET", "http://foo.example", nil)
|
|
request.Header["Accept"] = []string{fmt.Sprintf("%s,%s", api.OctetStreamMediaType, api.JsonMediaType)}
|
|
result := SszRequested(request)
|
|
assert.Equal(t, true, result)
|
|
})
|
|
|
|
t.Run("ssz_content_type_preferred_1", func(t *testing.T) {
|
|
request := httptest.NewRequest("GET", "http://foo.example", nil)
|
|
request.Header["Accept"] = []string{fmt.Sprintf("%s;q=0.9,%s", api.JsonMediaType, api.OctetStreamMediaType)}
|
|
result := SszRequested(request)
|
|
assert.Equal(t, true, result)
|
|
})
|
|
|
|
t.Run("ssz_content_type_preferred_2", func(t *testing.T) {
|
|
request := httptest.NewRequest("GET", "http://foo.example", nil)
|
|
request.Header["Accept"] = []string{fmt.Sprintf("%s;q=0.95,%s;q=0.9", api.OctetStreamMediaType, api.JsonMediaType)}
|
|
result := SszRequested(request)
|
|
assert.Equal(t, true, result)
|
|
})
|
|
|
|
t.Run("other_content_type_preferred", func(t *testing.T) {
|
|
request := httptest.NewRequest("GET", "http://foo.example", nil)
|
|
request.Header["Accept"] = []string{fmt.Sprintf("%s,%s;q=0.9", api.JsonMediaType, api.OctetStreamMediaType)}
|
|
result := SszRequested(request)
|
|
assert.Equal(t, false, result)
|
|
})
|
|
|
|
t.Run("other_params", func(t *testing.T) {
|
|
request := httptest.NewRequest("GET", "http://foo.example", nil)
|
|
request.Header["Accept"] = []string{fmt.Sprintf("%s,%s;q=0.9,otherparam=xyz", api.JsonMediaType, api.OctetStreamMediaType)}
|
|
result := SszRequested(request)
|
|
assert.Equal(t, false, result)
|
|
})
|
|
|
|
t.Run("no_header", func(t *testing.T) {
|
|
request := httptest.NewRequest("GET", "http://foo.example", nil)
|
|
result := SszRequested(request)
|
|
assert.Equal(t, false, result)
|
|
})
|
|
|
|
t.Run("empty_header", func(t *testing.T) {
|
|
request := httptest.NewRequest("GET", "http://foo.example", nil)
|
|
request.Header["Accept"] = []string{}
|
|
result := SszRequested(request)
|
|
assert.Equal(t, false, result)
|
|
})
|
|
|
|
t.Run("empty_header_value", func(t *testing.T) {
|
|
request := httptest.NewRequest("GET", "http://foo.example", nil)
|
|
request.Header["Accept"] = []string{""}
|
|
result := SszRequested(request)
|
|
assert.Equal(t, false, result)
|
|
})
|
|
|
|
t.Run("other_content_type", func(t *testing.T) {
|
|
request := httptest.NewRequest("GET", "http://foo.example", nil)
|
|
request.Header["Accept"] = []string{"application/other"}
|
|
result := SszRequested(request)
|
|
assert.Equal(t, false, result)
|
|
})
|
|
|
|
t.Run("garbage", func(t *testing.T) {
|
|
request := httptest.NewRequest("GET", "http://foo.example", nil)
|
|
request.Header["Accept"] = []string{"This is Sparta!!!"}
|
|
result := SszRequested(request)
|
|
assert.Equal(t, false, result)
|
|
})
|
|
}
|