Skip to content

Commit

Permalink
update purl
Browse files Browse the repository at this point in the history
  • Loading branch information
ejstreet committed Feb 26, 2023
1 parent d84ce15 commit 61c9e5c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
22 changes: 13 additions & 9 deletions omglol/purl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
)

// Create a PersistentURL object
Expand All @@ -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 := "<nil>"
if p.Counter != nil {
counter = strconv.Itoa(int(*p.Counter))
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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

Expand Down
53 changes: 45 additions & 8 deletions omglol/purl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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())
Expand All @@ -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())
Expand Down

0 comments on commit 61c9e5c

Please sign in to comment.