chore(http): use client for news feed

This commit is contained in:
jguer 2021-05-16 22:41:33 +02:00 committed by J Guerreiro
parent e20ab8b6a2
commit c7cf7baa48
3 changed files with 11 additions and 4 deletions

2
cmd.go
View file

@ -243,7 +243,7 @@ func handlePrint(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
case cmdArgs.ExistsArg("w", "news"): case cmdArgs.ExistsArg("w", "news"):
double := cmdArgs.ExistsDouble("w", "news") double := cmdArgs.ExistsDouble("w", "news")
quiet := cmdArgs.ExistsArg("q", "quiet") quiet := cmdArgs.ExistsArg("q", "quiet")
return news.PrintNewsFeed(dbExecutor.LastBuildTime(), config.SortMode, double, quiet) return news.PrintNewsFeed(config.Runtime.HTTPClient, dbExecutor.LastBuildTime(), config.SortMode, double, quiet)
case cmdArgs.ExistsDouble("c", "complete"): case cmdArgs.ExistsDouble("c", "complete"):
return completion.Show(dbExecutor, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, true) return completion.Show(dbExecutor, config.AURURL, config.Runtime.CompletionPath, config.CompletionInterval, true)
case cmdArgs.ExistsArg("c", "complete"): case cmdArgs.ExistsArg("c", "complete"):

View file

@ -2,6 +2,7 @@ package news
import ( import (
"bytes" "bytes"
"context"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"html" "html"
@ -59,8 +60,13 @@ type rss struct {
Channel channel `xml:"channel"` Channel channel `xml:"channel"`
} }
func PrintNewsFeed(cutOffDate time.Time, sortMode int, all, quiet bool) error { func PrintNewsFeed(client *http.Client, cutOffDate time.Time, sortMode int, all, quiet bool) error {
resp, err := http.Get("https://archlinux.org/feeds/news") req, err := http.NewRequestWithContext(context.Background(), "GET", "https://archlinux.org/feeds/news", nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil { if err != nil {
return err return err
} }

View file

@ -2,6 +2,7 @@ package news
import ( import (
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"testing" "testing"
"time" "time"
@ -110,7 +111,7 @@ func TestPrintNewsFeed(t *testing.T) {
r, w, _ := os.Pipe() r, w, _ := os.Pipe()
os.Stdout = w os.Stdout = w
err := PrintNewsFeed(tt.args.cutOffDate, tt.args.sortMode, tt.args.all, tt.args.quiet) err := PrintNewsFeed(&http.Client{}, tt.args.cutOffDate, tt.args.sortMode, tt.args.all, tt.args.quiet)
assert.NoError(t, err) assert.NoError(t, err)
w.Close() w.Close()