diff --git a/omglol/purl.go b/omglol/purl.go index 3615434..e0b66eb 100644 --- a/omglol/purl.go +++ b/omglol/purl.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "strconv" + "strings" ) // Create a PersistentURL object @@ -25,7 +26,7 @@ func NewPersistentURL(Name, URL string, listed bool, Counter ...*int64) *Persist } // Returns a string representaion of a PersistentURL -func (p *PersistentURL) ToString() string { +func (p *PersistentURL) String() string { counter := "" if p.Counter != nil { counter = strconv.Itoa(int(*p.Counter)) @@ -35,21 +36,21 @@ func (p *PersistentURL) ToString() string { // Create a new PersistentURL. See https://api.omg.lol/#token-post-purls-create-a-new-purl func (c *Client) CreatePersistentURL(domain string, purl PersistentURL) error { - + type purlRequest struct { - Name string `json:"name"` - URL string `json:"url"` - Listed *bool `json:"listed"` + Name string `json:"name"` + URL string `json:"url"` + Listed *bool `json:"listed"` } p := purlRequest{ Name: purl.Name, - URL: purl.URL, + URL: purl.URL, } if !purl.Listed { p.Listed = nil - } else { + } else { t := true p.Listed = &t } @@ -139,8 +140,13 @@ func (c *Client) ListPersistentURLs(address string) (*[]PersistentURL, error) { return nil, err } + var p []PersistentURL + body, err := c.doRequest(req) if err != nil { + if strings.Contains(err.Error(), "status: 404") { + return &p, nil + } return nil, err } @@ -166,8 +172,6 @@ func (c *Client) ListPersistentURLs(address string) (*[]PersistentURL, error) { return nil, err } - var p []PersistentURL - for _, purl := range r.Response.PURLs { var x PersistentURL diff --git a/omglol/purl_test.go b/omglol/purl_test.go index 9d84fdd..55fd62f 100644 --- a/omglol/purl_test.go +++ b/omglol/purl_test.go @@ -4,8 +4,20 @@ import ( "testing" ) +func validatePersistentURL(t *testing.T, p PersistentURL) { + if len(p.Name) <= 0 { + t.Error("PURL Name is empty.") + } + if len(p.URL) <= 0 { + t.Error("PURL URL is empty.") + } + if p.Counter == nil { + t.Error("PURL Counter should not be 'nil'.") + } +} + func TestGetPersistentURL(t *testing.T) { - t.Parallel() + sleep() c, err := NewClient(testEmail, testKey, testHostURL) if err != nil { @@ -17,11 +29,16 @@ func TestGetPersistentURL(t *testing.T) { t.Errorf(err.Error()) } - t.Logf(p.ToString()) + if p != nil { + t.Logf(p.String()) + validatePersistentURL(t, *p) + } else { + t.Error("GetPersistentURL returned 'nil'.") + } } func TestListPersistentURLs(t *testing.T) { - t.Parallel() + sleep() c, err := NewClient(testEmail, testKey, testHostURL) if err != nil { @@ -33,13 +50,19 @@ func TestListPersistentURLs(t *testing.T) { t.Errorf(err.Error()) } - for _, p := range *l { - t.Logf(p.ToString() + "\n") + if l != nil { + for _, p := range *l { + t.Logf(p.String() + "\n") + validatePersistentURL(t, p) + } + } else { + t.Error("ListPersistentURLs returned 'nil'.") } + } func TestCreateAndDeletePersistentURL(t *testing.T) { - t.Parallel() + sleep() c, err := NewClient(testEmail, testKey, testHostURL) if err != nil { @@ -55,11 +78,18 @@ func TestCreateAndDeletePersistentURL(t *testing.T) { t.Errorf(err.Error()) } - _, err = c.GetPersistentURL(testOwnedDomain, name1) + u, err := c.GetPersistentURL(testOwnedDomain, name1) if err != nil { t.Errorf(err.Error()) } + if u != nil { + t.Log(u.String()) + validatePersistentURL(t, *u) + } else { + t.Error("GetPersistentURL returned 'nil' when retrieving unlisted PURL.") + } + err = c.DeletePersistentURL(testOwnedDomain, name1) if err != nil { t.Errorf(err.Error()) @@ -74,11 +104,18 @@ func TestCreateAndDeletePersistentURL(t *testing.T) { t.Errorf(err.Error()) } - _, err = c.GetPersistentURL(testOwnedDomain, name2) + l, err := c.GetPersistentURL(testOwnedDomain, name2) if err != nil { t.Errorf(err.Error()) } + if l != nil { + t.Log(l.String()) + validatePersistentURL(t, *l) + } else { + t.Error("GetPersistentURL returned 'nil' when retrieving listed PURL.") + } + err = c.DeletePersistentURL(testOwnedDomain, name2) if err != nil { t.Errorf(err.Error())