Skip to content

Commit

Permalink
add tests specific to Corpus
Browse files Browse the repository at this point in the history
  • Loading branch information
jub0bs committed May 8, 2024
1 parent 07225bc commit 5e40061
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions internal/origins/corpus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package origins_test

import (
"slices"
"testing"

"github.com/jub0bs/cors/internal/origins"
)

func TestCorpus(t *testing.T) {
cases := []struct {
desc string
patterns []string
accepts []string
rejects []string
elems []string
}{
{
desc: "empty corpus",
rejects: []string{"https://example.com"},
}, {
desc: "one discrete origin",
patterns: []string{"https://example.com"},
accepts: []string{"https://example.com"},
rejects: []string{
"http://example.com",
"https://example.com:8080",
"https://example.org",
"https://foo.example.com",
},
elems: []string{"https://example.com"},
}, {
desc: "two discrete origins",
patterns: []string{
"https://example.org",
"https://example.com",
},
accepts: []string{
"https://example.org",
"https://example.com",
},
rejects: []string{
"http://example.org",
"https://example.org:8080",
"https://foo.example.org",
"http://example.com",
"https://example.com:8080",
"https://foo.example.com",
},
elems: []string{
"https://example.com",
"https://example.org",
},
}, {
desc: "one discrete origin and one wildcard origin pattern",
patterns: []string{
"https://*.example.org",
"https://example.com",
},
accepts: []string{
"https://foo.example.org",
"https://foo.bar.example.org",
"https://example.com",
},
rejects: []string{
"https://example.org",
"http://foo.example.org",
"http://foo.bar.example.org",
"https://example.org:8080",
"http://foo.example.org:8080",
"http://foo.bar.example.org:8080",
"http://example.com",
"https://example.com:8080",
"https://foo.example.com",
},
elems: []string{
"https://*.example.org",
"https://example.com",
},
},
}
for _, tc := range cases {
f := func(t *testing.T) {
corpus := make(origins.Corpus)
for _, raw := range tc.patterns {
pattern, err := origins.ParsePattern(raw)
if err != nil {
t.Fatalf("origins.ParsePatten(%q): got non-nil error; want nil", raw)
}
corpus.Add(&pattern)
}
for _, raw := range tc.accepts {
origin, ok := origins.Parse(raw)
if !ok {
t.Fatalf("origins.Parse(%q): got false; want true", raw)
}
if !corpus.Contains(&origin) {
t.Errorf("corpus.Contains(%q): got false; want true", raw)
}
}
for _, raw := range tc.rejects {
origin, ok := origins.Parse(raw)
if !ok {
t.Fatalf("origins.Parse(%q): got false; want true", raw)
}
if corpus.Contains(&origin) {
t.Errorf("corpus.Contains(%q): got true; want false", raw)
}
}
elems := corpus.Elems()
if !slices.Equal(elems, tc.elems) {
t.Errorf("corpus.Elems(): got %q; want %q", elems, tc.elems)
}
}
t.Run(tc.desc, f)
}
}

0 comments on commit 5e40061

Please sign in to comment.