-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Support RFC9110 * test: Add test cases for content type derivation and validation
- Loading branch information
Showing
3 changed files
with
115 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package api_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/ethpandaops/checkpointz/pkg/api" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDeriveContentType(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
accept string | ||
expected api.ContentType | ||
}{ | ||
{"JSON", "application/json", api.ContentTypeJSON}, | ||
{"Wildcard", "*/*", api.ContentTypeJSON}, | ||
{"YAML", "application/yaml", api.ContentTypeYAML}, | ||
{"SSZ", "application/octet-stream", api.ContentTypeSSZ}, | ||
{"Unknown", "application/unknown", api.ContentTypeUnknown}, | ||
{"Empty", "", api.ContentTypeJSON}, | ||
{"QValue JSON", "application/json;q=0.8", api.ContentTypeJSON}, | ||
{"QValue YAML", "application/yaml;q=0.5", api.ContentTypeYAML}, | ||
{"QValue Multiple", "application/json;q=0.8, application/yaml;q=0.5", api.ContentTypeJSON}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := api.DeriveContentType(tt.accept) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
func TestValidateContentType(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
contentType api.ContentType | ||
accepting []api.ContentType | ||
expectError bool | ||
}{ | ||
{"Valid JSON", api.ContentTypeJSON, []api.ContentType{api.ContentTypeJSON, api.ContentTypeYAML}, false}, | ||
{"Invalid JSON", api.ContentTypeJSON, []api.ContentType{api.ContentTypeYAML}, true}, | ||
{"Valid YAML", api.ContentTypeYAML, []api.ContentType{api.ContentTypeJSON, api.ContentTypeYAML}, false}, | ||
{"Invalid YAML", api.ContentTypeYAML, []api.ContentType{api.ContentTypeJSON}, true}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := api.ValidateContentType(tt.contentType, tt.accepting) | ||
if tt.expectError { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNewContentTypeFromRequest(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
accept string | ||
expected api.ContentType | ||
}{ | ||
{"JSON", "application/json", api.ContentTypeJSON}, | ||
{"Wildcard", "*/*", api.ContentTypeJSON}, | ||
{"YAML", "application/yaml", api.ContentTypeYAML}, | ||
{"SSZ", "application/octet-stream", api.ContentTypeSSZ}, | ||
{"Unknown", "application/unknown", api.ContentTypeJSON}, | ||
{"Empty", "", api.ContentTypeJSON}, | ||
{"QValue JSON", "application/json;q=0.8", api.ContentTypeJSON}, | ||
{"QValue YAML", "application/yaml;q=0.5", api.ContentTypeYAML}, | ||
{"QValue Multiple", "application/json;q=0.8, application/yaml;q=0.5", api.ContentTypeJSON}, | ||
{"Nimbus example", "application/octet-stream,application/json;q=0.9", api.ContentTypeSSZ}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req, err := http.NewRequest("GET", "http://example.com", http.NoBody) | ||
assert.NoError(t, err) | ||
req.Header.Set("Accept", tt.accept) | ||
|
||
result := api.NewContentTypeFromRequest(req) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters