Reuse weather API readiness response
This commit is contained in:
@@ -5,13 +5,19 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/config"
|
||||
)
|
||||
|
||||
func TestRunFetchesBundle(t *testing.T) {
|
||||
server := collectionTestServer(t, nil)
|
||||
var currentRequests atomic.Int32
|
||||
server := collectionTestServer(t, nil, func(r *http.Request) {
|
||||
if r.URL.Path == "/conditions/current" {
|
||||
currentRequests.Add(1)
|
||||
}
|
||||
})
|
||||
defer server.Close()
|
||||
|
||||
cfg := config.Defaults()
|
||||
@@ -30,6 +36,9 @@ func TestRunFetchesBundle(t *testing.T) {
|
||||
if result.Bundle.WeatherStory == nil || result.Bundle.WeatherStory.Title != "Several Chances for Rain Through Monday" {
|
||||
t.Fatalf("WeatherStory = %#v, want fetched weather story", result.Bundle.WeatherStory)
|
||||
}
|
||||
if got := currentRequests.Load(); got != 1 {
|
||||
t.Fatalf("conditions/current requests = %d, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunWrapsAdapterConstructionError(t *testing.T) {
|
||||
@@ -50,7 +59,7 @@ func TestRunWrapsAdapterConstructionError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunWrapsFetchError(t *testing.T) {
|
||||
server := collectionTestServer(t, map[string]int{"/observations": http.StatusBadRequest})
|
||||
server := collectionTestServer(t, map[string]int{"/observations": http.StatusBadRequest}, nil)
|
||||
defer server.Close()
|
||||
|
||||
cfg := config.Defaults()
|
||||
@@ -68,9 +77,12 @@ func TestRunWrapsFetchError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func collectionTestServer(t *testing.T, statusByPath map[string]int) *httptest.Server {
|
||||
func collectionTestServer(t *testing.T, statusByPath map[string]int, onRequest func(*http.Request)) *httptest.Server {
|
||||
t.Helper()
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if onRequest != nil {
|
||||
onRequest(r)
|
||||
}
|
||||
if status := statusByPath[r.URL.Path]; status != 0 {
|
||||
http.Error(w, "upstream failure", status)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user