-
Notifications
You must be signed in to change notification settings - Fork 6.2k
oss: detect ECS region and use internal endpoint automaticaly #65687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f1d458f
6c639ad
e63d1d0
ae747ce
df7ad51
aafde58
32a6436
6b17662
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,11 +20,9 @@ import ( | |
| "crypto/tls" | ||
| "database/sql" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "os" | ||
| "runtime" | ||
| "strconv" | ||
|
|
@@ -414,42 +412,6 @@ func SchemaExists(ctx context.Context, db dbutil.QueryExecutor, schema string) ( | |
| } | ||
| } | ||
|
|
||
| // GetJSON fetches a page and parses it as JSON. The parsed result will be | ||
| // stored into the `v`. The variable `v` must be a pointer to a type that can be | ||
| // unmarshalled from JSON. | ||
| // | ||
| // Example: | ||
| // | ||
| // client := &http.Client{} | ||
| // var resp struct { IP string } | ||
| // if err := util.GetJSON(client, "http://api.ipify.org/?format=json", &resp); err != nil { | ||
| // return errors.Trace(err) | ||
| // } | ||
| // fmt.Println(resp.IP) | ||
| func GetJSON(ctx context.Context, client *http.Client, url string, v any) error { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are 2 such method, they are merged into one in |
||
| req, err := http.NewRequestWithContext(ctx, "GET", url, nil) | ||
| if err != nil { | ||
| return errors.Trace(err) | ||
| } | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return errors.Trace(err) | ||
| } | ||
|
|
||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return errors.Trace(err) | ||
| } | ||
| return errors.Errorf("get %s http status code != 200, message %s", url, string(body)) | ||
| } | ||
|
|
||
| return errors.Trace(json.NewDecoder(resp.Body).Decode(v)) | ||
| } | ||
|
|
||
| // KillMySelf sends sigint to current process, used in integration test only | ||
| // | ||
| // Only works on Unix. Signaling on Windows is not supported. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,13 +17,9 @@ package common_test | |
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/DATA-DOG/go-sqlmock" | ||
| "github.com/go-sql-driver/mysql" | ||
|
|
@@ -44,46 +40,6 @@ func TestDirNotExist(t *testing.T) { | |
| require.False(t, common.IsDirExists("not-exists")) | ||
| } | ||
|
|
||
| func TestGetJSON(t *testing.T) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to |
||
| type TestPayload struct { | ||
| Username string `json:"username"` | ||
| Password string `json:"password"` | ||
| } | ||
| request := TestPayload{ | ||
| Username: "lightning", | ||
| Password: "lightning-ctl", | ||
| } | ||
|
|
||
| ctx := context.Background() | ||
| // Mock success response | ||
| handle := func(res http.ResponseWriter, _ *http.Request) { | ||
| res.WriteHeader(http.StatusOK) | ||
| err := json.NewEncoder(res).Encode(request) | ||
| require.NoError(t, err) | ||
| } | ||
| testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
| handle(res, req) | ||
| })) | ||
| defer testServer.Close() | ||
|
|
||
| client := &http.Client{Timeout: time.Second} | ||
|
|
||
| response := TestPayload{} | ||
| err := common.GetJSON(ctx, client, "http://localhost:1", &response) | ||
| require.Error(t, err) | ||
| err = common.GetJSON(ctx, client, testServer.URL, &response) | ||
| require.NoError(t, err) | ||
| require.Equal(t, request, response) | ||
|
|
||
| // Mock `StatusNoContent` response | ||
| handle = func(res http.ResponseWriter, _ *http.Request) { | ||
| res.WriteHeader(http.StatusNoContent) | ||
| } | ||
| err = common.GetJSON(ctx, client, testServer.URL, &response) | ||
| require.Error(t, err) | ||
| require.Regexp(t, ".*http status code != 200.*", err.Error()) | ||
| } | ||
|
|
||
| func TestConnect(t *testing.T) { | ||
| plainPsw := "dQAUoDiyb1ucWZk7" | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to
pkg/util/httputil/