Skip to content

Commit

Permalink
fix cursor, support ?specversion as a capability, add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Davis <[email protected]>
  • Loading branch information
duglin committed Jan 4, 2025
1 parent 06ee0d2 commit 8a4f609
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
14 changes: 7 additions & 7 deletions registry/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Capabilities struct {
var AllowableFlags = ArrayToLower([]string{
"epoch", "export", "filter", "inline",
"nested", "nodefaultversionid", "nodefaultversionsticky",
"noepoch", "noreadonly", "schema", "setdefaultversionid"})
"noepoch", "noreadonly", "schema", "setdefaultversionid", "specversion"})

var AllowableMutable = ArrayToLower([]string{
"capabilities", "entities", "model"})
Expand Down Expand Up @@ -135,10 +135,10 @@ func (c *Capabilities) Validate() error {
return err
}

if !ArrayContains(c.Schemas, strings.ToLower(XREGSCHEMA+"/"+SPECVERSION)) {
if !ArrayContainsAnyCase(c.Schemas, XREGSCHEMA+"/"+SPECVERSION) {
return fmt.Errorf(`"schemas" must contain %q`, XREGSCHEMA+"/"+SPECVERSION)
}
if !ArrayContains(c.SpecVersions, strings.ToLower(SPECVERSION)) {
if !ArrayContainsAnyCase(c.SpecVersions, SPECVERSION) {
return fmt.Errorf(`"specversions" must contain %q`, SPECVERSION)
}

Expand All @@ -160,25 +160,25 @@ func ParseCapabilitiesJSON(buf []byte) (*Capabilities, error) {
}

func (c *Capabilities) FlagEnabled(str string) bool {
return ArrayContains(c.Flags, strings.ToLower(str))
return ArrayContainsAnyCase(c.Flags, str)
}

func (c *Capabilities) MutableEnabled(str string) bool {
return ArrayContains(c.Mutable, strings.ToLower(str))
return ArrayContainsAnyCase(c.Mutable, str)
}

func (c *Capabilities) PaginationEnabled() bool {
return c.Pagination
}

func (c *Capabilities) SchemaEnabled(str string) bool {
return ArrayContains(c.Schemas, strings.ToLower(str))
return ArrayContainsAnyCase(c.Schemas, str)
}

func (c *Capabilities) ShortSelfEnabled(str string) bool {
return c.ShortSelf
}

func (c *Capabilities) SpecVersionEnabled(str string) bool {
return ArrayContains(c.SpecVersions, strings.ToLower(str))
return ArrayContainsAnyCase(c.SpecVersions, str)
}
10 changes: 10 additions & 0 deletions registry/httpStuff.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
"the model \"hasdocument\" value set to \"false\" is invalid")
}

if err == nil {
if sv := info.GetFlag("specversion"); sv != "" {
if !info.Registry.Capabilities.SpecVersionEnabled(sv) {
err = fmt.Errorf("Unsupported xRegistry spec version: %s",
sv)
}
}
}

if err == nil {
// These should only return an error if they didn't already
// send a response back to the client.
Expand Down Expand Up @@ -565,6 +574,7 @@ func (pw *PageWriter) Done() {
}
.expandBtn {
cursor: default ;
display: inline-block ;
width: 2ch ;
text-align: center ;
Expand Down
10 changes: 10 additions & 0 deletions registry/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,16 @@ func ArrayContains(strs []string, needle string) bool {
return false
}

func ArrayContainsAnyCase(strs []string, needle string) bool {
needle = strings.ToLower(needle)
for _, s := range strs {
if needle == strings.ToLower(s) {
return true
}
}
return false
}

// Convert a string into a unique MD5 string - basically just for cases
// where we want to create a tiny URL
func MD5(str string) string {
Expand Down
29 changes: 19 additions & 10 deletions tests/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func TestCapabilitySimple(t *testing.T) {
"noepoch",
"noreadonly",
"schema",
"setdefaultversionid"
"setdefaultversionid",
"specversion"
],
"mutable": [
"capabilities",
Expand Down Expand Up @@ -64,7 +65,8 @@ func TestCapabilitySimple(t *testing.T) {
"noepoch",
"noreadonly",
"schema",
"setdefaultversionid"
"setdefaultversionid",
"specversion"
],
"mutable": [
"capabilities",
Expand Down Expand Up @@ -256,7 +258,8 @@ func TestCapabilityPath(t *testing.T) {
"noepoch",
"noreadonly",
"schema",
"setdefaultversionid"
"setdefaultversionid",
"specversion"
],
"mutable": [
"capabilities",
Expand Down Expand Up @@ -370,7 +373,7 @@ func TestCapabilityPath(t *testing.T) {
"flags": [
"epoch", "export", "filter", "inline", "nested", "nodefaultversionid",
"nodefaultversionsticky", "noepoch", "noreadonly", "schema",
"setdefaultversionid"
"setdefaultversionid", "specversion"
],
"mutable": [ "capabilities", "entities", "model" ],
"pagination": false,
Expand All @@ -390,7 +393,8 @@ func TestCapabilityPath(t *testing.T) {
"noepoch",
"noreadonly",
"schema",
"setdefaultversionid"
"setdefaultversionid",
"specversion"
],
"mutable": [
"capabilities",
Expand Down Expand Up @@ -420,7 +424,8 @@ func TestCapabilityPath(t *testing.T) {
"noepoch",
"noreadonly",
"schema",
"setdefaultversionid"
"setdefaultversionid",
"specversion"
],
"mutable": [
"capabilities",
Expand Down Expand Up @@ -617,7 +622,7 @@ func TestCapabilityAttr(t *testing.T) {
"flags": [
"epoch", "export", "filter", "inline", "nested", "nodefaultversionid",
"nodefaultversionsticky", "noepoch", "noreadonly", "schema",
"setdefaultversionid"
"setdefaultversionid", "specversion"
],
"mutable": [ "capabilities", "entities", "model" ],
"pagination": false,
Expand Down Expand Up @@ -648,7 +653,8 @@ func TestCapabilityAttr(t *testing.T) {
"noepoch",
"noreadonly",
"schema",
"setdefaultversionid"
"setdefaultversionid",
"specversion"
],
"mutable": [
"capabilities",
Expand Down Expand Up @@ -735,9 +741,9 @@ func TestCapabilityAttr(t *testing.T) {

// "epoch", "export", "filter", "inline",
// "nested", "nodefaultversionid", "nodefaultversionsticky",
// "noepoch", "noreadonly", "schema", "setdefaultversionid"})
// "noepoch", "noreadonly", "schema", "setdefaultversionid", "specversion"})

func TestCapabilityFlags(t *testing.T) {
func TestCapabilityFlagsOff(t *testing.T) {
reg := NewRegistry("TestCapabilityFlags")
defer PassDeleteReg(t, reg)

Expand Down Expand Up @@ -825,5 +831,8 @@ func TestCapabilityFlags(t *testing.T) {
// Test ?schema
xHTTP(t, reg, "GET", "/model?schema=foo", ``, 200, `*`)

// Test ?specversion
xHTTP(t, reg, "GET", "/model?specversion=foo", ``, 200, `*`)

// TODO nodefaultversionid, nodefaultversionsticky, noepoch, noreadonly
}
9 changes: 9 additions & 0 deletions tests/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14727,3 +14727,12 @@ func TestHTTPInvalidID(t *testing.T) {
`{"fileid": "f1*"}`, 400,
"\"f1*\" isn't a valid ID\n")
}

func TestHTTPSpecVersion(t *testing.T) {
reg := NewRegistry("TestHTTPSpecVersion")
defer PassDeleteReg(t, reg)

xHTTP(t, reg, "GET", "?specversion=0.5", "", 200, "*")
xHTTP(t, reg, "GET", "?specversion=0.x", "", 400,
"Unsupported xRegistry spec version: 0.x\n")
}

0 comments on commit 8a4f609

Please sign in to comment.