-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
Ting
committed
May 20, 2024
1 parent
59c6238
commit 158cf65
Showing
72 changed files
with
1,701 additions
and
99 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
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,21 @@ | ||
package ehttp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/gotomicro/ego/core/elog" | ||
) | ||
|
||
func TestNewComponent(t *testing.T) { | ||
// Normal case | ||
t.Run("Normal case", func(t *testing.T) { | ||
config := &Config{Addr: "http://hello.com"} | ||
logger := elog.DefaultLogger | ||
component := newComponent("hello", config, logger) | ||
assert.Equal(t, "hello", component.name) | ||
}) | ||
|
||
// Other case... | ||
} |
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,33 @@ | ||
package ehttp | ||
|
||
import ( | ||
"reflect" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/gotomicro/ego/core/util/xtime" | ||
) | ||
|
||
func Test_DefaultConfig(t *testing.T) { | ||
assert.True(t, reflect.DeepEqual(&Config{ | ||
Addr: "", | ||
Debug: false, | ||
RawDebug: false, | ||
ReadTimeout: xtime.Duration("2s"), | ||
SlowLogThreshold: xtime.Duration("500ms"), | ||
IdleConnTimeout: 90 * time.Second, | ||
MaxIdleConns: 100, | ||
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1, | ||
EnableTraceInterceptor: true, | ||
EnableKeepAlives: true, | ||
EnableAccessInterceptor: false, | ||
EnableAccessInterceptorRes: false, | ||
EnableMetricInterceptor: false, | ||
PathRelabel: nil, | ||
cookieJar: nil, | ||
httpClient: nil, | ||
}, DefaultConfig())) | ||
} |
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,5 @@ | ||
[test] | ||
"name" = "hello" | ||
|
||
[test1] | ||
"name" = "world" |
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 ehttp | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/gotomicro/ego/core/econf" | ||
) | ||
|
||
func TestLoad(t *testing.T) { | ||
file, err := os.Open("./config_test/conf.toml") | ||
assert.NoError(t, err) | ||
err = econf.LoadFromReader(file, toml.Unmarshal) | ||
assert.NoError(t, err) | ||
container := Load("test").Build().name | ||
assert.Equal(t, "test", container) | ||
} |
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,74 @@ | ||
package ehttp | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/gotomicro/ego/core/elog" | ||
) | ||
|
||
func TestLogAccess(t *testing.T) { | ||
name := "test" | ||
config := &Config{} | ||
logger := &elog.Component{} | ||
u, err := url.Parse("https://hello.com/xxx") | ||
assert.NoError(t, err) | ||
ctx := context.WithValue(context.Background(), urlKey{}, u) | ||
req := resty.New().R().SetContext(ctx) | ||
res := &resty.Response{} | ||
logAccess(name, config, logger, req, res, err) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestBeg(t *testing.T) { | ||
ctx := context.Background() | ||
now := time.Now() | ||
ctx = context.WithValue(ctx, begKey{}, now) | ||
|
||
result := beg(ctx) | ||
assert.Equal(t, now, result) | ||
} | ||
|
||
type CustomResolver struct { | ||
Address string | ||
} | ||
|
||
func (r *CustomResolver) GetAddr() string { | ||
return r.Address | ||
} | ||
|
||
func TestFixedInterceptor(t *testing.T) { | ||
name := "test" | ||
config := &Config{} | ||
logger := &elog.Component{} | ||
builder := &CustomResolver{Address: "https://test.com"} | ||
|
||
client := resty.New() | ||
request := client.R() | ||
request.SetContext(context.Background()) | ||
request.URL = "https://hello.com/world" | ||
middleware, _, _ := fixedInterceptor(name, config, logger, builder) | ||
|
||
// case 1 | ||
config.Addr = "" | ||
err := middleware(client, request) | ||
assert.NoError(t, err) | ||
|
||
// case 2 | ||
config.Addr = "https://xxxxx.com/xxx" | ||
err = middleware(client, request) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "https://test.com", client.HostURL) | ||
} | ||
|
||
func TestFileWithLineNum(t *testing.T) { | ||
file := "/usr/local/go/src/testing/testing.go" | ||
got := fileWithLineNum() | ||
assert.True(t, true, strings.HasPrefix(got, file)) | ||
} |
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,53 @@ | ||
package ehttp | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestOptions(t *testing.T) { | ||
c := &Container{ | ||
config: &Config{}, | ||
} | ||
expectedAddr := "127.0.0.1:8080" | ||
expectedReadTimeOut := time.Duration(5) | ||
expectedSlowLogThreshold := time.Duration(5) | ||
expectedIdleConnTimeOut := time.Duration(5) | ||
|
||
WithAddr(expectedAddr)(c) | ||
WithDebug(true)(c) | ||
WithRawDebug(false)(c) | ||
WithReadTimeout(expectedReadTimeOut)(c) | ||
WithSlowLogThreshold(expectedSlowLogThreshold)(c) | ||
WithIdleConnTimeout(expectedIdleConnTimeOut)(c) | ||
WithMaxIdleConns(3)(c) | ||
WithMaxIdleConnsPerHost(3)(c) | ||
WithEnableTraceInterceptor(true)(c) | ||
WithEnableKeepAlives(true)(c) | ||
WithEnableMetricInterceptor(true)(c) | ||
WithEnableAccessInterceptor(true)(c) | ||
WithEnableAccessInterceptorRes(true)(c) | ||
WithPathRelabel("hello", "test")(c) | ||
WithJar(nil)(c) | ||
WithHTTPClient(nil)(c) | ||
|
||
assert.Equal(t, expectedAddr, c.config.Addr) | ||
assert.Equal(t, true, c.config.Debug) | ||
assert.Equal(t, false, c.config.RawDebug) | ||
assert.Equal(t, expectedReadTimeOut, c.config.ReadTimeout) | ||
assert.Equal(t, expectedSlowLogThreshold, c.config.SlowLogThreshold) | ||
assert.Equal(t, expectedIdleConnTimeOut, c.config.IdleConnTimeout) | ||
assert.Equal(t, 3, c.config.MaxIdleConns) | ||
assert.Equal(t, 3, c.config.MaxIdleConnsPerHost) | ||
assert.Equal(t, true, c.config.EnableTraceInterceptor) | ||
assert.Equal(t, true, c.config.EnableKeepAlives) | ||
assert.Equal(t, true, c.config.EnableMetricInterceptor) | ||
assert.Equal(t, true, c.config.EnableAccessInterceptor) | ||
assert.Equal(t, true, c.config.EnableAccessInterceptorRes) | ||
reflect.DeepEqual(Relabel{Match: "hello", Replacement: "test"}, c.config.PathRelabel) | ||
assert.Equal(t, nil, c.config.cookieJar) | ||
reflect.DeepEqual(nil, c.config.httpClient) | ||
} |
Oops, something went wrong.