Skip to content

Commit

Permalink
Unit test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmaddox committed Oct 23, 2024
1 parent 055ac9e commit edeced8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 9 additions & 0 deletions golink.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,15 @@ func serveMine(w http.ResponseWriter, r *http.Request) {
return
}

// Return JSON if the client doesn't accept HTML
if !acceptHTML(r) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(links); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}

mineTmpl.Execute(w, links)
}

Expand Down
13 changes: 7 additions & 6 deletions golink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,11 @@ func TestServeMine(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// Seed the database with links
db.Save(&Link{Short: "link1", Long: "http://example.com/1", Owner: "user1"})
db.Save(&Link{Short: "link2", Long: "http://example.com/2", Owner: "user2"})
db.Save(&Link{Short: "link3", Long: "http://example.com/3", Owner: "user1"})

// Seed the database with links - update Owner to match login format
db.Save(&Link{Short: "link1", Long: "http://example.com/1", Owner: "[email protected]"})
db.Save(&Link{Short: "link2", Long: "http://example.com/2", Owner: "[email protected]"})
db.Save(&Link{Short: "link3", Long: "http://example.com/3", Owner: "[email protected]"})

tests := []struct {
name string
Expand All @@ -637,8 +638,8 @@ func TestServeMine(t *testing.T) {
return user{login: "[email protected]"}, nil
},
wantLinks: []*Link{
{Short: "link1", Long: "http://example.com/1", Owner: "user1"},
{Short: "link3", Long: "http://example.com/3", Owner: "user1"},
{Short: "link1", Long: "http://example.com/1", Owner: "user1@example.com"},
{Short: "link3", Long: "http://example.com/3", Owner: "user1@example.com"},
},
wantStatus: http.StatusOK,
},
Expand Down

0 comments on commit edeced8

Please sign in to comment.