Skip to content
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: response body after HTTP error should be closed #106

Merged
merged 8 commits into from
Sep 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
chore: remove unnecessary tests
karel-rehor committed Sep 27, 2024

Verified

This commit was signed with the committer’s verified signature.
kareefardi Kareem Farid
commit 5ab799221024f04b7ba4d9cb2d9a1f55a044c1a9
35 changes: 0 additions & 35 deletions influxdb3/client_e2e_test.go
Original file line number Diff line number Diff line change
@@ -26,13 +26,10 @@
package influxdb3_test

import (
"bytes"
"context"
"fmt"
"log/slog"
"os"
"strconv"
"strings"
"testing"
"time"

@@ -364,35 +361,3 @@ func TestEscapedStringValues(t *testing.T) {
assert.EqualValues(t, "escaped\\nline and space", qit.Value()["tag2"])
}
}

func TestRespBodyClosedAfterError(t *testing.T) {
SkipCheck(t)
url := os.Getenv("TESTING_INFLUXDB_URL")
token := os.Getenv("TESTING_INFLUXDB_TOKEN")
database := os.Getenv("TESTING_INFLUXDB_DATABASE")

buffer := bytes.NewBuffer(make([]byte, 256))
logger := slog.New(slog.NewTextHandler(buffer, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
origLogger := slog.Default()
slog.SetDefault(logger)
defer func() {
b := bytes.Trim(buffer.Bytes(), "\x00")
logged := strings.Split(string(b), "\n")
assert.Regexp(t,
".*level=DEBUG msg=\"Closed response body on HTTP Error\\(invalid: no data written, errors encountered on line\\(s\\): line 1: No fields were provided\\)\"",
logged[0])
slog.SetLogLoggerLevel(slog.LevelInfo)
slog.SetDefault(origLogger)
}()

client, err := influxdb3.New(influxdb3.ClientConfig{
Host: url,
Token: token,
Database: database,
})

err = client.Write(context.Background(), []byte("test,type=negative val="))
require.Error(t, err)
}
47 changes: 0 additions & 47 deletions influxdb3/client_test.go
Original file line number Diff line number Diff line change
@@ -23,10 +23,7 @@
package influxdb3

import (
"bytes"
"context"
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
@@ -626,47 +623,3 @@ func TestFixUrl(t *testing.T) {
})
}
}

func TestRespCloseOnError(t *testing.T) {
slog.SetLogLoggerLevel(slog.LevelDebug)
buffer := bytes.NewBuffer(make([]byte, 256))
logger := slog.New(slog.NewTextHandler(buffer, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
origLogger := slog.Default()
slog.SetDefault(logger)
defer func() {
b := bytes.Trim(buffer.Bytes(), "\x00")
logged := strings.Split(string(b), "\n")
assert.Regexp(t,
".*level=DEBUG msg=\"Closed response body on HTTP Error\\(Not Found: The page you are looking for was not found\\).*",
logged[0])
slog.SetLogLoggerLevel(slog.LevelInfo)
slog.SetDefault(origLogger)
}()
msg := "{ \"code\": \"Not Found\", \"message\": \"The page you are looking for was not found\", \"data\": null }"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(msg))
}))
defer ts.Close()
client, err := New(ClientConfig{Host: ts.URL, Token: "my-token"})
require.NoError(t, err)
turl, err := url.Parse(ts.URL)
require.NoError(t, err)
res, callErr := client.makeAPICall(context.Background(), httpParams{ //nolint:bodyclose
endpointURL: turl,
queryParams: nil,
httpMethod: "GET",
headers: nil,
body: nil,
})

if callErr != nil {
slog.Info(callErr.Error())
assert.EqualValues(t, "Not Found: The page you are looking for was not found", callErr.Error())
} else {
assert.Fail(t, fmt.Sprintf("Call should have returned error. But got res %s", res.Body))
}
}