From 6059782de810b37fa9e08f55936679222cd873f1 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 29 Mar 2020 19:36:49 +0800 Subject: [PATCH] add unit testing case of basic auth for ghttp.Client; remove intlog for New function of package gspath --- .example/other/test.go | 28 +++++++++++----------------- net/ghttp/ghttp_client_config.go | 4 ++-- net/ghttp/ghttp_unit_client_test.go | 24 ++++++++++++++++++++++++ os/gspath/gspath.go | 2 +- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/.example/other/test.go b/.example/other/test.go index d9382c9d3..c1e9ba98d 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -1,25 +1,19 @@ package main import ( - "github.com/gogf/gf/crypto/gaes" - "github.com/gogf/gf/os/gfile" - "github.com/gogf/gf/os/gres" -) - -var ( - CryptoKey = []byte("x76cgqt36i9c863bzmotuf8626dxiwu0") + "fmt" + "github.com/gogf/gf/frame/g" ) func main() { - binContent, err := gres.Pack("public,config") - if err != nil { - panic(err) - } - binContent, err = gaes.Encrypt(binContent, CryptoKey) - if err != nil { - panic(err) - } - if err := gfile.PutBytes("data.bin", binContent); err != nil { - panic(err) + c := g.Client() + c.SetCookieMap(g.MapStrStr{ + "name": "john", + "score": "100", + }) + if r, e := c.Get("http://127.0.0.1:8199/"); e != nil { + panic(e) + } else { + fmt.Println(r.ReadAllString()) } } diff --git a/net/ghttp/ghttp_client_config.go b/net/ghttp/ghttp_client_config.go index dc2978a57..d650b7f19 100644 --- a/net/ghttp/ghttp_client_config.go +++ b/net/ghttp/ghttp_client_config.go @@ -9,8 +9,8 @@ package ghttp import ( "context" "crypto/tls" + "github.com/gogf/gf/text/gstr" "net/http" - "strings" "time" "github.com/gogf/gf/text/gregex" @@ -94,7 +94,7 @@ func (c *Client) SetContentType(contentType string) *Client { // SetHeaderRaw sets custom HTTP header using raw string. func (c *Client) SetHeaderRaw(headers string) *Client { - for _, line := range strings.Split(strings.TrimSpace(headers), "\n") { + for _, line := range gstr.SplitAndTrim(headers, "\n") { array, _ := gregex.MatchString(`^([\w\-]+):\s*(.+)`, line) if len(array) >= 3 { c.header[array[1]] = array[2] diff --git a/net/ghttp/ghttp_unit_client_test.go b/net/ghttp/ghttp_unit_client_test.go index 2608cdc64..74c631bf6 100644 --- a/net/ghttp/ghttp_unit_client_test.go +++ b/net/ghttp/ghttp_unit_client_test.go @@ -42,6 +42,30 @@ func Test_Client_Basic(t *testing.T) { }) } +func Test_Client_BasicAuth(t *testing.T) { + p := ports.PopRand() + s := g.Server(p) + s.BindHandler("/auth", func(r *ghttp.Request) { + if r.BasicAuth("admin", "admin") { + r.Response.Write("1") + } else { + r.Response.Write("0") + } + }) + s.SetPort(p) + s.SetDumpRouterMap(false) + s.Start() + defer s.Shutdown() + + time.Sleep(100 * time.Millisecond) + gtest.C(t, func(t *gtest.T) { + c := g.Client() + c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) + t.Assert(c.BasicAuth("admin", "123").GetContent("/auth"), "0") + t.Assert(c.BasicAuth("admin", "admin").GetContent("/auth"), "1") + }) +} + func Test_Client_Cookie(t *testing.T) { p := ports.PopRand() s := g.Server(p) diff --git a/os/gspath/gspath.go b/os/gspath/gspath.go index 0ab5b2634..a2aafccb2 100644 --- a/os/gspath/gspath.go +++ b/os/gspath/gspath.go @@ -52,7 +52,7 @@ func New(path string, cache bool) *SPath { } if len(path) > 0 { if _, err := sp.Add(path); err != nil { - intlog.Print(err) + //intlog.Print(err) } } return sp