Skip to content

Commit

Permalink
feat: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-timothy-albert committed Feb 14, 2024
1 parent 90a440c commit 1562840
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func main() {
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)
r.HandleFunc("/pagination/url", pagination.HandleURL).Methods(http.MethodGet)
r.HandleFunc("/retries", retries.HandleRetries).Methods(http.MethodGet, http.MethodPost)
r.HandleFunc("/retries/after", retries.HandleRetries).Methods(http.MethodGet)
r.HandleFunc("/errors/{status_code}", errors.HandleErrors).Methods(http.MethodGet)
r.HandleFunc("/optional", acceptHeaders.HandleAcceptHeaderMultiplexing).Methods(http.MethodGet)
r.HandleFunc("/readonlyorwriteonly", readonlywriteonly.HandleReadOrWrite).Methods(http.MethodPost)
Expand Down
11 changes: 9 additions & 2 deletions internal/pagination/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func HandleCursor(w http.ResponseWriter, r *http.Request) {

func HandleURL(w http.ResponseWriter, r *http.Request) {
attemptsString := r.FormValue("attempts")
isReferencePath := r.FormValue("is-reference-path")
var attempts int
if attemptsString != "" {
var err error
Expand All @@ -137,9 +138,15 @@ func HandleURL(w http.ResponseWriter, r *http.Request) {
}

if attempts > 1 {
baseURL := fmt.Sprintf("%s://%s%s", r.URL.Scheme, r.Host, r.URL.Path)
baseURL := fmt.Sprintf("%s://%s", r.URL.Scheme, r.Host)
if r.URL.Scheme == "" { // Fallback if Scheme is not available
baseURL = fmt.Sprintf("http://%s%s", r.Host, r.URL.Path)
baseURL = fmt.Sprintf("http://%s", r.Host)
}

if isReferencePath == "true" {
baseURL = r.URL.Path
} else {
baseURL = fmt.Sprintf("%s%s", baseURL, r.URL.Path)
}

nextUrl := fmt.Sprintf("%s?attempts=%d", baseURL, attempts-1)
Expand Down
17 changes: 15 additions & 2 deletions internal/retries/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ type retriesResponse struct {
func HandleRetries(w http.ResponseWriter, r *http.Request) {
requestID := r.URL.Query().Get("request-id")
numRetriesStr := r.URL.Query().Get("num-retries")
retryAfterVal := r.URL.Query().Get("retry-after-val")

retryAfter := 0
if retryAfterVal != "" {
var err error
retryAfter, err = strconv.Atoi(retryAfterVal)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("retry-after-val must be an integer"))
return
}
}

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

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

0 comments on commit 1562840

Please sign in to comment.