-
Notifications
You must be signed in to change notification settings - Fork 136
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
8 changed files
with
156 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package httpc | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/fx/fxtest" | ||
|
||
"github.com/pingcap/tidb-dashboard/pkg/config" | ||
) | ||
|
||
func newTestClient(t *testing.T) *Client { | ||
lc := fxtest.NewLifecycle(t) | ||
config := &config.Config{} | ||
return NewHTTPClient(lc, config) | ||
} | ||
|
||
func Test_Clone(t *testing.T) { | ||
c := newTestClient(t) | ||
cc := c.Clone() | ||
|
||
require.NotSame(t, c, cc) | ||
|
||
require.Nil(t, c.header) | ||
require.Nil(t, cc.header) | ||
require.NotSame(t, c.header, cc.header) | ||
} | ||
|
||
func Test_CloneAndAddRequestHeader(t *testing.T) { | ||
c := newTestClient(t) | ||
cc := c.CloneAndAddRequestHeader("1", "11") | ||
|
||
require.Nil(t, c.header) | ||
require.Equal(t, "11", cc.header.Get("1")) | ||
|
||
cc2 := cc.CloneAndAddRequestHeader("2", "22") | ||
require.Equal(t, "11", cc.header.Get("1")) | ||
require.Equal(t, "", cc.header.Get("2")) | ||
require.Equal(t, "11", cc2.header.Get("1")) | ||
require.Equal(t, "22", cc2.header.Get("2")) | ||
} | ||
|
||
func Test_Send_withHeader(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte(r.Header.Get("1"))) | ||
})) | ||
defer ts.Close() | ||
|
||
c := newTestClient(t) | ||
resp1, _ := c.Send(context.Background(), ts.URL, http.MethodGet, nil, nil, "") | ||
d1, _ := resp1.Body() | ||
require.Equal(t, "", string(d1)) | ||
|
||
cc := c.CloneAndAddRequestHeader("1", "11") | ||
resp2, _ := cc.Send(context.Background(), ts.URL, http.MethodGet, nil, nil, "") | ||
d2, _ := resp2.Body() | ||
require.Equal(t, "11", string(d2)) | ||
|
||
resp3, _ := c.Send(context.Background(), ts.URL, http.MethodGet, nil, nil, "") | ||
d3, _ := resp3.Body() | ||
require.Equal(t, "", string(d3)) | ||
} |
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,52 @@ | ||
// Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package pd | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/fx/fxtest" | ||
|
||
"github.com/pingcap/tidb-dashboard/pkg/config" | ||
"github.com/pingcap/tidb-dashboard/pkg/httpc" | ||
) | ||
|
||
func newTestClient(t *testing.T) *Client { | ||
lc := fxtest.NewLifecycle(t) | ||
config := &config.Config{} | ||
c := NewPDClient(lc, httpc.NewHTTPClient(lc, config), config) | ||
c.lifecycleCtx = context.Background() | ||
return c | ||
} | ||
|
||
func Test_AddRequestHeader_returnDifferentHTTPClient(t *testing.T) { | ||
c := newTestClient(t) | ||
cc := c.AddRequestHeader("1", "11") | ||
|
||
require.NotSame(t, c.httpClient, cc.httpClient) | ||
} | ||
|
||
func Test_Get_withHeader(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte(r.Header.Get("1"))) | ||
})) | ||
defer ts.Close() | ||
|
||
c := newTestClient(t).WithBaseURL(ts.URL) | ||
resp1, _ := c.Get("") | ||
d1, _ := resp1.Body() | ||
require.Equal(t, "", string(d1)) | ||
|
||
cc := c.AddRequestHeader("1", "11") | ||
resp2, _ := cc.Get("") | ||
d2, _ := resp2.Body() | ||
require.Equal(t, "11", string(d2)) | ||
|
||
resp3, _ := c.Get("") | ||
d3, _ := resp3.Body() | ||
require.Equal(t, "", string(d3)) | ||
} |
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,3 +1,3 @@ | ||
# This file specifies the TiDB Dashboard internal version, which will be printed in `--version` | ||
# and UI. In release branch, changing this file will result in publishing a new version and tag. | ||
2021.11.08.1 | ||
2021.12.06.1 |