Skip to content

Commit

Permalink
arXiv feed test
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Mar 21, 2024
1 parent 8b8fd8a commit 5ccb81b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/arxiv/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package arxiv
import (
"encoding/xml"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand All @@ -29,7 +29,7 @@ func GetResults(url string) (Feed, error) {
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return result, err
Expand Down
46 changes: 46 additions & 0 deletions pkg/arxiv/feed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package arxiv_test

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/lehigh-university-libraries/papercut/pkg/arxiv"
)

func TestGetResults(t *testing.T) {
// Mocking HTTP server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Respond with a sample Atom feed XML
xml := `<feed xmlns="http://www.w3.org/2005/Atom" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<link/>
<title>Test Feed</title>
<id>urn:uuid:60a76c80-74b1-11eb-9439-0242ac130002</id>
<updated>2023-10-30T10:00:00Z</updated>
<opensearch:totalResults>2</opensearch:totalResults>
<opensearch:startIndex>1</opensearch:startIndex>
<opensearch:itemsPerPage>10</opensearch:itemsPerPage>
<entry></entry>
<entry></entry>
</feed>`
w.Header().Set("Content-Type", "application/xml")
fmt.Fprintln(w, xml)
}))
defer ts.Close()

// Test case
url := ts.URL
feed, err := arxiv.GetResults(url)
if err != nil {
t.Errorf("GetResults(%s) returned error: %v", url, err)
}

// Verify the result
if feed.Title != "Test Feed" {
t.Errorf("Expected feed title 'Test Feed', got '%s'", feed.Title)
}
if feed.TotalResults != 2 {
t.Errorf("Expected totalResults to be 2, got %d", feed.TotalResults)
}
}

0 comments on commit 5ccb81b

Please sign in to comment.