plugins/tests/http_test.go
2024-02-18 13:23:11 +08:00

72 lines
1.8 KiB
Go

package tests
import (
"apigo.cloud/git/apigo/gojs"
_ "apigo.cloud/git/apigo/plugins/http"
"github.com/ssgo/s"
"strings"
"testing"
)
func TestHttp(t *testing.T) {
s.Register(0, "/echo", func(in map[string]interface{}) map[string]interface{} {
return in
}, "")
s.Register(0, "/jump", func(response *s.Response) {
response.Location("/echo?aaa=222")
}, "")
s.Config.Listen = "10803"
as := s.AsyncStart()
defer as.Stop()
rt := gojs.New(nil, nil)
defer rt.Close()
r, err, _ := rt.Run(`
r = http.get('http://127.0.0.1:10803/echo?aaa=111')
if(r.aaa != 111) throw new Error('aaa is '+r.aaa+' not 111')
return true
`)
Test(t, "get", r == true && err == nil, r, err)
r, err, _ = rt.Run(`
r = http.post('http://127.0.0.1:10803/echo', {aaa:111})
if(r.aaa != 111) throw new Error('aaa is '+r.aaa+' not 111')
return true
`)
Test(t, "post", r == true && err == nil, r, err)
r, err, _ = rt.Run(`
r = http.get('http://127.0.0.1:10803/jump')
if(r.aaa != 222) throw new Error('aaa is '+r.aaa+' not 222')
return true
`)
Test(t, "jump", r == true && err == nil, r, err)
}
func TestH2C(t *testing.T) {
s.Register(0, "/echo", func(in map[string]interface{}) map[string]interface{} {
return in
}, "")
s.Config.Listen = ""
as := s.AsyncStart()
defer as.Stop()
rt := gojs.New(nil, nil)
defer rt.Close()
rt.JsCtx.Globals().Set("server", rt.JsCtx.String(as.Addr))
_, err, _ := rt.Run(`
http.get('http://'+server+'/echo?aaa=111')
`)
Test(t, "get for h2c", err != nil && strings.Contains(err.Error(), "transport connection broken"))
r, err, _ := rt.Run(`
let c = http.newH2C(10)
r = c.post('http://'+server+'/echo', {aaa:111})
if(r.aaa != 111) throw new Error('aaa is '+r.aaa+' not 111')
return true
`)
Test(t, "post for h2c", r == true && err == nil, r, err)
}