Skip to content

Commit 34a87ef

Browse files
authored
test: add unit tests for validateMediaType (#946)
Add some test cases for `validateMediaType` in `pack_test.go`. Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
1 parent f7a6126 commit 34a87ef

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

pack_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,3 +1090,105 @@ func Test_PackManifest_UnsupportedPackManifestVersion(t *testing.T) {
10901090
t.Errorf("Oras.PackManifest() error = %v, wantErr = %v", err, wantErr)
10911091
}
10921092
}
1093+
1094+
func Test_validateMediaType(t *testing.T) {
1095+
tests := []struct {
1096+
name string
1097+
mediaType string
1098+
wantErr bool
1099+
}{
1100+
{
1101+
name: "valid media type - common",
1102+
mediaType: "application/vnd.oci.image.manifest.v1+json",
1103+
wantErr: false,
1104+
},
1105+
{
1106+
name: "valid media type - without +",
1107+
mediaType: "text/plain",
1108+
wantErr: false,
1109+
},
1110+
{
1111+
name: "valid media type - json+ld",
1112+
mediaType: "application/json+ld",
1113+
wantErr: false,
1114+
},
1115+
{
1116+
name: "valid media type - with dot",
1117+
mediaType: "application/x.foo",
1118+
wantErr: false,
1119+
},
1120+
{
1121+
name: "valid media type - with dash",
1122+
mediaType: "application/x-foo",
1123+
wantErr: false,
1124+
},
1125+
{
1126+
name: "valid media type - with caret",
1127+
mediaType: "application/vnd.foo^bar",
1128+
wantErr: false,
1129+
},
1130+
{
1131+
name: "invalid media type - empty string",
1132+
mediaType: "",
1133+
wantErr: true,
1134+
},
1135+
{
1136+
name: "invalid media type - missing subtype",
1137+
mediaType: "application/",
1138+
wantErr: true,
1139+
},
1140+
{
1141+
name: "invalid media type - missing type",
1142+
mediaType: "/vnd.oci.image.manifest.v1",
1143+
wantErr: true,
1144+
},
1145+
{
1146+
name: "invalid media type - no slash",
1147+
mediaType: "applicationvnd.oci.image.manifest.v1",
1148+
wantErr: true,
1149+
},
1150+
{
1151+
name: "invalid media type - multiple slashes",
1152+
mediaType: "application/something/v1",
1153+
wantErr: true,
1154+
},
1155+
{
1156+
name: "invalid media type - type starts with non-alphanumeric",
1157+
mediaType: "-application/vnd.oci.image.manifest.v1",
1158+
wantErr: true,
1159+
},
1160+
{
1161+
name: "invalid media type - subtype starts with non-alphanumeric",
1162+
mediaType: "application/-vnd.oci.image.manifest.v1",
1163+
wantErr: true,
1164+
},
1165+
{
1166+
name: "invalid media type - invalid char in type",
1167+
mediaType: "application%/vnd.oci.image.manifest.v1",
1168+
wantErr: true,
1169+
},
1170+
{
1171+
name: "invalid media type - invalid char in subtype",
1172+
mediaType: "application/vnd.oci.image.manifest.v1%json",
1173+
wantErr: true,
1174+
},
1175+
{
1176+
name: "invalid media type - contains space",
1177+
mediaType: "application/vnd oci",
1178+
wantErr: true,
1179+
},
1180+
}
1181+
1182+
for _, tt := range tests {
1183+
t.Run(tt.name, func(t *testing.T) {
1184+
err := validateMediaType(tt.mediaType)
1185+
if (err != nil) != tt.wantErr {
1186+
t.Errorf("validateMediaType(%q) error = %v, wantErr %v", tt.mediaType, err, tt.wantErr)
1187+
return
1188+
}
1189+
if tt.wantErr && !errors.Is(err, errdef.ErrInvalidMediaType) {
1190+
t.Errorf("validateMediaType(%q) error not wrapping errdef.ErrInvalidMediaType: %v", tt.mediaType, err)
1191+
}
1192+
})
1193+
}
1194+
}

0 commit comments

Comments
 (0)