Skip to content

Commit

Permalink
update code and add fixes found in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Oct 1, 2017
1 parent 2bb5c96 commit eb2c6ce
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
35 changes: 35 additions & 0 deletions test/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,38 @@ func TestToValues(t *testing.T) {
t.Fail()
}
}

func TestParseReferenceURI(t *testing.T) {
cases := map[string]client.Target{
"/api/content?type=Test&id=1": client.Target{Type: "Test", ID: 1},
}

for in, expected := range cases {
got, err := client.ParseReferenceURI(in)
if err != nil {
fmt.Println(err)
t.Fail()
}

if got.ID != expected.ID {
fmt.Printf("expected: %v got: %v\n", expected.ID, got.ID)
t.Fail()
}
}
}

func TestParseReferenceURIErrors(t *testing.T) {
cases := map[string]string{
"/api/content": "improperly formatted reference URI: /api/content",
"/api/content?type=Test&noID=1": "reference URI missing 'id' value: /api/content?type=Test&noID=1",
"/api/content?noType=Test&id=1": "reference URI missing 'type' value: /api/content?noType=Test&id=1",
}

for in, expected := range cases {
_, err := client.ParseReferenceURI(in)
if err.Error() != expected {
fmt.Printf("got: %v, expected: %s\n", err, expected)
t.Fail()
}
}
}
11 changes: 7 additions & 4 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,24 @@ func ParseReferenceURI(uri string) (Target, error) {
}

func parseReferenceURI(uri string) (Target, error) {
if !strings.HasPrefix(uri, "/api/content?") {
const prefix = "/api/content?"
if !strings.HasPrefix(uri, prefix) {
return Target{}, fmt.Errorf("improperly formatted reference URI: %s", uri)
}

uri = strings.TrimPrefix(uri, prefix)

q, err := url.ParseQuery(uri)
if err != nil {
return Target{}, fmt.Errorf("failed to parse reference URI: %s, %v", uri, err)
return Target{}, fmt.Errorf("failed to parse reference URI: %s, %v", prefix+uri, err)
}

if q.Get("type") == "" {
return Target{}, fmt.Errorf("reference URI missing 'type' value: %s", uri)
return Target{}, fmt.Errorf("reference URI missing 'type' value: %s", prefix+uri)
}

if q.Get("id") == "" {
return Target{}, fmt.Errorf("reference URI missing 'id' value: %s", uri)
return Target{}, fmt.Errorf("reference URI missing 'id' value: %s", prefix+uri)
}

// convert query id string to int
Expand Down

0 comments on commit eb2c6ce

Please sign in to comment.