Skip to content

Commit 1562840

Browse files
feat: update tests
1 parent 90a440c commit 1562840

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

cmd/server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func main() {
3535
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)
3636
r.HandleFunc("/pagination/url", pagination.HandleURL).Methods(http.MethodGet)
3737
r.HandleFunc("/retries", retries.HandleRetries).Methods(http.MethodGet, http.MethodPost)
38+
r.HandleFunc("/retries/after", retries.HandleRetries).Methods(http.MethodGet)
3839
r.HandleFunc("/errors/{status_code}", errors.HandleErrors).Methods(http.MethodGet)
3940
r.HandleFunc("/optional", acceptHeaders.HandleAcceptHeaderMultiplexing).Methods(http.MethodGet)
4041
r.HandleFunc("/readonlyorwriteonly", readonlywriteonly.HandleReadOrWrite).Methods(http.MethodPost)

internal/pagination/service.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ func HandleCursor(w http.ResponseWriter, r *http.Request) {
120120

121121
func HandleURL(w http.ResponseWriter, r *http.Request) {
122122
attemptsString := r.FormValue("attempts")
123+
isReferencePath := r.FormValue("is-reference-path")
123124
var attempts int
124125
if attemptsString != "" {
125126
var err error
@@ -137,9 +138,15 @@ func HandleURL(w http.ResponseWriter, r *http.Request) {
137138
}
138139

139140
if attempts > 1 {
140-
baseURL := fmt.Sprintf("%s://%s%s", r.URL.Scheme, r.Host, r.URL.Path)
141+
baseURL := fmt.Sprintf("%s://%s", r.URL.Scheme, r.Host)
141142
if r.URL.Scheme == "" { // Fallback if Scheme is not available
142-
baseURL = fmt.Sprintf("http://%s%s", r.Host, r.URL.Path)
143+
baseURL = fmt.Sprintf("http://%s", r.Host)
144+
}
145+
146+
if isReferencePath == "true" {
147+
baseURL = r.URL.Path
148+
} else {
149+
baseURL = fmt.Sprintf("%s%s", baseURL, r.URL.Path)
143150
}
144151

145152
nextUrl := fmt.Sprintf("%s?attempts=%d", baseURL, attempts-1)

internal/retries/service.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ type retriesResponse struct {
1515
func HandleRetries(w http.ResponseWriter, r *http.Request) {
1616
requestID := r.URL.Query().Get("request-id")
1717
numRetriesStr := r.URL.Query().Get("num-retries")
18+
retryAfterVal := r.URL.Query().Get("retry-after-val")
19+
20+
retryAfter := 0
21+
if retryAfterVal != "" {
22+
var err error
23+
retryAfter, err = strconv.Atoi(retryAfterVal)
24+
if err != nil {
25+
w.WriteHeader(http.StatusBadRequest)
26+
_, _ = w.Write([]byte("retry-after-val must be an integer"))
27+
return
28+
}
29+
}
1830

1931
numRetries := 3
2032
if numRetriesStr != "" {
@@ -40,8 +52,9 @@ func HandleRetries(w http.ResponseWriter, r *http.Request) {
4052
callCounts[requestID]++
4153

4254
if callCounts[requestID] < numRetries {
43-
// sets a static one second retry after timeout, the client will decide whether or not to respect it
44-
w.Header().Set("Retry-After", "1")
55+
if retryAfter > 0 {
56+
w.Header().Set("Retry-After", strconv.Itoa(retryAfter))
57+
}
4558
w.WriteHeader(http.StatusServiceUnavailable)
4659
_, _ = w.Write([]byte("request failed please retry"))
4760
return

0 commit comments

Comments
 (0)