From ac39a6d17365d4c20fc5daf3db989cf51bb08fe3 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Mon, 16 Oct 2023 15:48:37 +0200 Subject: [PATCH] Make subscription tests more robust. Fix some issues --- subscription.go | 2 +- subscription_test.go | 49 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/subscription.go b/subscription.go index af5b8a0..6526b29 100644 --- a/subscription.go +++ b/subscription.go @@ -89,7 +89,7 @@ func (s *HTTPSubscriber) downloadAndAppend() { case (resp.StatusCode >= http.StatusBadRequest && resp.StatusCode != http.StatusRequestedRangeNotSatisfiable) || resp.StatusCode >= http.StatusInternalServerError: - logger.Errorf("%s: server returned with unexpected code %s", resp.StatusCode) + logger.Errorf("%s: server returned with unexpected code %d", s.LocalFile, resp.StatusCode) // error is ignored, we continued subscribed } } diff --git a/subscription_test.go b/subscription_test.go index 4170001..a18e462 100644 --- a/subscription_test.go +++ b/subscription_test.go @@ -13,10 +13,49 @@ func createTestServer() *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Simulate serving a file from the server data := []byte("This is the remote content.") - w.Header().Set("Content-Range", fmt.Sprintf("bytes 0-%d/%d", len(data)-1, len(data))) - w.Header().Set("Content-Length", fmt.Sprint(len(data))) - w.WriteHeader(http.StatusPartialContent) - _, _ = w.Write(data) + contentLength := len(data) + + // Get the "Range" header from the request + rangeHeader := r.Header.Get("Range") + + if rangeHeader != "" { + var start, end int + + // Check for a range request in the format "bytes %d-" + if n, _ := fmt.Sscanf(rangeHeader, "bytes=%d-", &start); n == 1 { + // Handle open end range requests + if start >= contentLength { + http.Error(w, "Invalid Range header", http.StatusRequestedRangeNotSatisfiable) + return + } + end = contentLength - 1 + } else if n, _ := fmt.Sscanf(rangeHeader, "bytes=%d-%d", &start, &end); n == 2 { + // Check for valid byte range + if start < 0 || end >= contentLength || start > end { + http.Error(w, "Invalid Range header", http.StatusRequestedRangeNotSatisfiable) + return + } + } else { + http.Error(w, "Invalid Range header", http.StatusBadRequest) + return + } + + // Calculate the content range and length for the response + contentRange := fmt.Sprintf("bytes %d-%d/%d", start, end, contentLength) + w.Header().Set("Content-Range", contentRange) + w.Header().Set("Content-Length", fmt.Sprint(end-start+1)) + w.WriteHeader(http.StatusPartialContent) + + // Write the selected byte range to the response + _, _ = w.Write(data[start : end+1]) + } else { + // If no "Range" header, serve the entire content + w.Header().Set("Content-Range", fmt.Sprintf("bytes 0-%d/%d", contentLength-1, contentLength)) + w.Header().Set("Content-Length", fmt.Sprint(contentLength)) + w.WriteHeader(http.StatusPartialContent) + + _, _ = w.Write(data) + } })) } @@ -31,7 +70,7 @@ func TestHTTPSubscriber(t *testing.T) { go subscriber.Subscribe() // Allow some time for subscription to run - time.Sleep(time.Second) + time.Sleep(2 * time.Second) subscriber.Stop() localFileContent, err := os.ReadFile(localFile)