2020-11-26 08:57:54 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2021-01-05 07:35:38 +03:00
|
|
|
"encoding/json"
|
2020-11-26 08:57:54 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2021-01-16 02:49:58 +03:00
|
|
|
mhttp "github.com/unistack-org/micro-client-http/v3"
|
|
|
|
jsoncodec "github.com/unistack-org/micro-codec-json/v3"
|
2021-01-29 16:47:26 +03:00
|
|
|
rrouter "github.com/unistack-org/micro-router-register/v3"
|
2021-01-19 04:15:57 +03:00
|
|
|
pb "github.com/unistack-org/micro-tests/client/http/proto"
|
2020-11-26 08:57:54 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2021-01-29 16:47:26 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2020-11-26 08:57:54 +03:00
|
|
|
"github.com/unistack-org/micro/v3/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultHTTPCodecs = map[string]codec.Codec{
|
|
|
|
"application/json": jsoncodec.NewCodec(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
Seq int64
|
|
|
|
Data string
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:00:31 +03:00
|
|
|
type GithubRsp struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:35:38 +03:00
|
|
|
type GithubRspError struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
DocumentationUrl string `json:"documentation_url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GithubRspError) Error() string {
|
|
|
|
b, _ := json.Marshal(e)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2021-01-19 04:15:57 +03:00
|
|
|
func TestError(t *testing.T) {
|
2021-01-05 07:35:38 +03:00
|
|
|
c := client.NewClientCallOptions(mhttp.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsoncodec.NewCodec())), client.WithAddress("https://api.github.com"))
|
2021-03-24 11:12:31 +03:00
|
|
|
req := c.NewRequest("github", "/dddd", &pb.LookupUserReq{})
|
2021-01-05 07:35:38 +03:00
|
|
|
rsp := &GithubRsp{}
|
|
|
|
errMap := map[string]interface{}{"404": &GithubRspError{}}
|
|
|
|
err := c.Call(context.TODO(), req, rsp, mhttp.Method(http.MethodGet), mhttp.ErrorMap(errMap))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("request must return non nil err")
|
|
|
|
}
|
|
|
|
if _, ok := err.(*GithubRspError); !ok {
|
2021-03-24 11:12:31 +03:00
|
|
|
t.Fatalf("invalid response received: %T is not *GithubRspError type: %#+v", err, err)
|
2021-01-05 07:35:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:00:31 +03:00
|
|
|
func TestNative(t *testing.T) {
|
|
|
|
c := client.NewClientCallOptions(mhttp.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsoncodec.NewCodec())), client.WithAddress("https://api.github.com"))
|
2021-02-27 13:51:39 +03:00
|
|
|
gh := pb.NewGithubClient("github", c)
|
2021-01-19 04:15:57 +03:00
|
|
|
|
|
|
|
rsp, err := gh.LookupUser(context.TODO(), &pb.LookupUserReq{Username: "vtolstov"})
|
2020-12-16 00:00:31 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-01-19 04:15:57 +03:00
|
|
|
|
2020-12-16 00:00:31 +03:00
|
|
|
if rsp.Name != "Vasiliy Tolstov" {
|
2021-01-19 04:15:57 +03:00
|
|
|
t.Fatalf("invalid rsp received: %#+v\n", rsp)
|
2020-12-16 00:00:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-26 08:57:54 +03:00
|
|
|
func TestHTTPClient(t *testing.T) {
|
2021-02-12 20:50:02 +03:00
|
|
|
reg := register.NewRegister()
|
2021-01-29 16:47:26 +03:00
|
|
|
rtr := rrouter.NewRouter(router.Register(reg))
|
2020-11-26 08:57:54 +03:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/foo/bar", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// only accept post
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "expect post method", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// get codec
|
|
|
|
ct := r.Header.Get("Content-Type")
|
|
|
|
codec, ok := defaultHTTPCodecs[ct]
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "codec not found", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract message
|
|
|
|
msg := &Message{}
|
|
|
|
if err := codec.Unmarshal(b, msg); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal response
|
|
|
|
b, err = codec.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// write response
|
|
|
|
w.Write(b)
|
|
|
|
})
|
|
|
|
go http.Serve(l, mux)
|
|
|
|
|
2021-01-29 16:47:26 +03:00
|
|
|
if err := reg.Register(ctx, ®ister.Service{
|
2020-11-26 08:57:54 +03:00
|
|
|
Name: "test.service",
|
2021-01-29 16:47:26 +03:00
|
|
|
Nodes: []*register.Node{
|
2020-11-26 08:57:54 +03:00
|
|
|
{
|
|
|
|
Id: "test.service.1",
|
|
|
|
Address: l.Addr().String(),
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := mhttp.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsoncodec.NewCodec()), client.Router(rtr))
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
msg := &Message{
|
|
|
|
Seq: int64(i),
|
|
|
|
Data: fmt.Sprintf("message %d", i),
|
|
|
|
}
|
|
|
|
req := c.NewRequest("test.service", "/foo/bar", msg)
|
|
|
|
rsp := &Message{}
|
|
|
|
err := c.Call(context.TODO(), req, rsp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if rsp.Seq != msg.Seq {
|
|
|
|
t.Fatalf("invalid seq %d for %d", rsp.Seq, msg.Seq)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPClientStream(t *testing.T) {
|
2021-02-12 20:50:02 +03:00
|
|
|
reg := register.NewRegister()
|
2021-01-29 16:47:26 +03:00
|
|
|
rtr := rrouter.NewRouter(router.Register(reg))
|
2020-11-26 08:57:54 +03:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/foo/bar", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// only accept post
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "expect post method", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// hijack the connection
|
|
|
|
hj, ok := w.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "could not hijack conn", 500)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// hijacked
|
|
|
|
conn, bufrw, err := hj.Hijack()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// read off the first request
|
|
|
|
// get codec
|
|
|
|
ct := r.Header.Get("Content-Type")
|
|
|
|
codec, ok := defaultHTTPCodecs[ct]
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "codec not found", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract message
|
|
|
|
msg := &Message{}
|
|
|
|
if err := codec.Unmarshal(b, msg); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal response
|
|
|
|
b, err = codec.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// write response
|
|
|
|
rsp := &http.Response{
|
|
|
|
Header: r.Header,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewBuffer(b)),
|
|
|
|
Status: "200 OK",
|
|
|
|
StatusCode: 200,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
ContentLength: int64(len(b)),
|
|
|
|
}
|
|
|
|
|
|
|
|
// write response
|
|
|
|
rsp.Write(bufrw)
|
|
|
|
bufrw.Flush()
|
|
|
|
|
|
|
|
reader := bufio.NewReader(conn)
|
|
|
|
|
|
|
|
for {
|
|
|
|
r, err := http.ReadRequest(reader)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract message
|
|
|
|
msg := &Message{}
|
|
|
|
if err := codec.Unmarshal(b, msg); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal response
|
|
|
|
b, err = codec.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp := &http.Response{
|
|
|
|
Header: r.Header,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewBuffer(b)),
|
|
|
|
Status: "200 OK",
|
|
|
|
StatusCode: 200,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
ContentLength: int64(len(b)),
|
|
|
|
}
|
|
|
|
|
|
|
|
// write response
|
|
|
|
rsp.Write(bufrw)
|
|
|
|
bufrw.Flush()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go http.Serve(l, mux)
|
|
|
|
|
2021-01-29 16:47:26 +03:00
|
|
|
if err := reg.Register(ctx, ®ister.Service{
|
2020-11-26 08:57:54 +03:00
|
|
|
Name: "test.service",
|
2021-01-29 16:47:26 +03:00
|
|
|
Nodes: []*register.Node{
|
2020-11-26 08:57:54 +03:00
|
|
|
{
|
|
|
|
Id: "test.service.1",
|
|
|
|
Address: l.Addr().String(),
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := mhttp.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsoncodec.NewCodec()), client.Router(rtr))
|
|
|
|
req := c.NewRequest("test.service", "/foo/bar", &Message{})
|
|
|
|
stream, err := c.Stream(context.TODO(), req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer stream.Close()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
msg := &Message{
|
|
|
|
Seq: int64(i),
|
|
|
|
Data: fmt.Sprintf("message %d", i),
|
|
|
|
}
|
|
|
|
err := stream.Send(msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rsp := &Message{}
|
|
|
|
err = stream.Recv(rsp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if rsp.Seq != msg.Seq {
|
|
|
|
t.Fatalf("invalid seq %d for %d", rsp.Seq, msg.Seq)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|