-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
92 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package http | ||
|
||
import ( | ||
"github.com/DefangLabs/defang/src/pkg/term" | ||
"github.com/hashicorp/go-retryablehttp" | ||
) | ||
|
||
var DefaultClient = newClient().StandardClient() | ||
|
||
type termLogger struct{} | ||
|
||
func (termLogger) Printf(format string, args ...interface{}) { | ||
term.Debugf(format, args...) | ||
} | ||
|
||
func newClient() *retryablehttp.Client { | ||
c := retryablehttp.NewClient() // default client retries 4 times: 1+2+4+8 = 15s max | ||
c.Logger = termLogger{} | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,37 @@ | ||
package http | ||
|
||
import "testing" | ||
|
||
func TestRemoveQueryParam(t *testing.T) { | ||
url := "https://example.com/foo?bar=baz" | ||
expected := "https://example.com/foo" | ||
actual := RemoveQueryParam(url) | ||
if actual != expected { | ||
t.Errorf("expected %q, got %q", expected, actual) | ||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestPutRetries(t *testing.T) { | ||
const body = "test" | ||
calls := 0 | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
calls++ | ||
if calls < 3 { | ||
http.Error(w, "error", http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
if b, err := io.ReadAll(r.Body); err != nil || string(b) != body { | ||
t.Error("expected body to be read") | ||
} | ||
})) | ||
t.Cleanup(server.Close) | ||
|
||
resp, err := Put(context.Background(), server.URL, "text/plain", strings.NewReader(body)) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
if calls != 3 { | ||
t.Errorf("expected 3 calls, got %d", calls) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package http | ||
|
||
import "net/url" | ||
|
||
func RemoveQueryParam(qurl string) string { | ||
u, err := url.Parse(qurl) | ||
if err != nil { | ||
return qurl | ||
} | ||
u.RawQuery = "" | ||
return u.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package http | ||
|
||
import "testing" | ||
|
||
func TestRemoveQueryParam(t *testing.T) { | ||
url := "https://example.com/foo?bar=baz" | ||
expected := "https://example.com/foo" | ||
actual := RemoveQueryParam(url) | ||
if actual != expected { | ||
t.Errorf("expected %q, got %q", expected, actual) | ||
} | ||
} |