-
Notifications
You must be signed in to change notification settings - Fork 115
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
fix: Allow connecting to a server on a URL path (#149) #152
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,45 @@ package influxdb2 | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
http2 "github.com/influxdata/influxdb-client-go/internal/http" | ||
iwrite "github.com/influxdata/influxdb-client-go/internal/write" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUrls(t *testing.T) { | ||
urls := []struct { | ||
serverURL string | ||
serverAPIURL string | ||
writeURLPrefix string | ||
}{ | ||
{"http://host:9999", "http://host:9999/api/v2/", "http://host:9999/api/v2/write"}, | ||
{"http://host:9999/", "http://host:9999/api/v2/", "http://host:9999/api/v2/write"}, | ||
{"http://host:9999/path", "http://host:9999/path/api/v2/", "http://host:9999/path/api/v2/write"}, | ||
{"http://host:9999/path/", "http://host:9999/path/api/v2/", "http://host:9999/path/api/v2/write"}, | ||
{"http://host:9999/path1/path2/path3", "http://host:9999/path1/path2/path3/api/v2/", "http://host:9999/path1/path2/path3/api/v2/write"}, | ||
{"http://host:9999/path1/path2/path3/", "http://host:9999/path1/path2/path3/api/v2/", "http://host:9999/path1/path2/path3/api/v2/write"}, | ||
} | ||
for _, url := range urls { | ||
t.Run(url.serverURL, func(t *testing.T) { | ||
c := NewClient(url.serverURL, "x") | ||
ci := c.(*clientImpl) | ||
assert.Equal(t, url.serverURL, ci.serverUrl) | ||
assert.Equal(t, url.serverAPIURL, ci.httpService.ServerApiUrl()) | ||
ws := iwrite.NewService("org", "bucket", ci.httpService, c.Options().WriteOptions()) | ||
wu, err := ws.WriteUrl() | ||
require.Nil(t, err) | ||
assert.Equal(t, url.writeURLPrefix+"?bucket=bucket&org=org&precision=ns", wu) | ||
}) | ||
} | ||
} | ||
|
||
func TestUserAgent(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(100 * time.Millisecond) | ||
|
@@ -42,7 +71,7 @@ func TestServerError429(t *testing.T) { | |
w.Header().Set("Retry-After", "1") | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusTooManyRequests) | ||
w.Write([]byte(`{"code":"too many requests", "message":"exceeded rate limit"}`)) | ||
_, _ = w.Write([]byte(`{"code":"too many requests", "message":"exceeded rate limit"}`)) | ||
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. Why these blanks? Probably a violation of https://staticcheck.io/docs/checks#S1005 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. Explicitly noting about ignoring returned val and returned error recommended by the style check in the IDE. |
||
})) | ||
|
||
defer server.Close() | ||
|
@@ -56,11 +85,27 @@ func TestServerError429(t *testing.T) { | |
assert.Equal(t, "exceeded rate limit", perror.Message) | ||
} | ||
|
||
func TestServerOnPath(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == "/proxy/0:0/influx/api/v2/write" { | ||
w.WriteHeader(http.StatusNoContent) | ||
} else { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
_, _ = w.Write([]byte(fmt.Sprintf(`{"code":"internal server error", "message":"%s"}`, r.URL.Path))) | ||
} | ||
})) | ||
|
||
defer server.Close() | ||
c := NewClient(server.URL+"/proxy/0:0/influx/", "x") | ||
err := c.WriteApiBlocking("o", "b").WriteRecord(context.Background(), "a,a=a a=1i") | ||
require.Nil(t, err) | ||
} | ||
|
||
func TestServerErrorNonJSON(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(100 * time.Millisecond) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(`internal server error`)) | ||
_, _ = w.Write([]byte(`internal server error`)) | ||
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. |
||
})) | ||
|
||
defer server.Close() | ||
|
@@ -79,7 +124,7 @@ func TestServerErrorInflux1_8(t *testing.T) { | |
w.Header().Set("X-Influxdb-Error", "bruh moment") | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(404) | ||
w.Write([]byte(`{"error": "bruh moment"}`)) | ||
_, _ = w.Write([]byte(`{"error": "bruh moment"}`)) | ||
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. |
||
})) | ||
|
||
defer server.Close() | ||
|
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.
this line (and the next one, which is the same) is IMHO redundant
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.
/path1/path2/path3
is tested as a more complex path and they differ in the '/' at the end