@@ -1383,3 +1383,61 @@ func gzipBytes(t *testing.T, b []byte) []byte {
13831383 require .NoError (t , zw .Close ())
13841384 return buf .Bytes ()
13851385}
1386+
1387+ func TestRequestAcceptsJSON (t * testing.T ) {
1388+ for _ , tc := range []struct {
1389+ Header string
1390+ Expected bool
1391+ }{
1392+ {Header : "" , Expected : true },
1393+ {Header : "application/json" , Expected : true },
1394+ {Header : "application/octet-stream" , Expected : false },
1395+ {Header : "application/octet-stream;q=1.0,application/json;q=0.9" , Expected : true },
1396+ {Header : "application/octet-stream;q=1.0,application/something-else;q=0.9" , Expected : false },
1397+ {Header : "application/octet-stream;q=1.0,application/*;q=0.9" , Expected : true },
1398+ {Header : "application/octet-stream;q=1.0,*/*;q=0.9" , Expected : true },
1399+ {Header : "application/*;q=0.9" , Expected : true },
1400+ {Header : "application/*" , Expected : true },
1401+ } {
1402+ t .Run (tc .Header , func (t * testing.T ) {
1403+ req , err := http .NewRequest (http .MethodGet , "/eth/v1/builder/header/1/0x00/0xaa" , nil )
1404+ require .NoError (t , err )
1405+ req .Header .Set ("Accept" , tc .Header )
1406+ actual := RequestAcceptsJSON (req )
1407+ require .Equal (t , tc .Expected , actual )
1408+ })
1409+ }
1410+ }
1411+
1412+ func TestNegotiateRequestResponseType (t * testing.T ) {
1413+ for _ , tc := range []struct {
1414+ Header string
1415+ Expected string
1416+ Error error
1417+ }{
1418+ {Header : "" , Expected : ApplicationJSON },
1419+ {Header : "application/json" , Expected : ApplicationJSON },
1420+ {Header : "application/octet-stream" , Expected : ApplicationOctetStream },
1421+ {Header : "application/octet-stream;q=1.0,application/json;q=0.9" , Expected : ApplicationOctetStream },
1422+ {Header : "application/octet-stream;q=1.0,application/something-else;q=0.9" , Expected : ApplicationOctetStream },
1423+ {Header : "application/octet-stream;q=1.0,application/*;q=0.9" , Expected : ApplicationOctetStream },
1424+ {Header : "application/octet-stream;q=1.0,*/*;q=0.9" , Expected : ApplicationOctetStream },
1425+ {Header : "application/octet-stream;q=0.9,*/*;q=1.0" , Expected : ApplicationJSON },
1426+ {Header : "application/*;q=0.9" , Expected : ApplicationJSON , Error : nil },
1427+ {Header : "application/*" , Expected : ApplicationJSON , Error : nil },
1428+ {Header : "text/html" , Error : ErrNotAcceptable },
1429+ } {
1430+ t .Run (tc .Header , func (t * testing.T ) {
1431+ req , err := http .NewRequest (http .MethodGet , "/eth/v1/builder/header/1/0x00/0xaa" , nil )
1432+ require .NoError (t , err )
1433+ req .Header .Set ("Accept" , tc .Header )
1434+ negotiated , err := NegotiateRequestResponseType (req )
1435+ if tc .Error != nil {
1436+ require .Equal (t , tc .Error , err )
1437+ } else {
1438+ require .NoError (t , err )
1439+ require .Equal (t , tc .Expected , negotiated )
1440+ }
1441+ })
1442+ }
1443+ }
0 commit comments