Skip to content

Commit

Permalink
Make subscription tests more robust. Fix some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan committed Oct 16, 2023
1 parent eeab36e commit ac39a6d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
49 changes: 44 additions & 5 deletions subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}))
}

Expand All @@ -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)
Expand Down

0 comments on commit ac39a6d

Please sign in to comment.