add unit testing case of basic auth for ghttp.Client; remove intlog for New function of package gspath

This commit is contained in:
John 2020-03-29 19:36:49 +08:00
parent 4844eea0ab
commit 6059782de8
4 changed files with 38 additions and 20 deletions

View File

@ -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())
}
}

View File

@ -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]

View File

@ -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)

View File

@ -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