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-10-27 01:24:35 +03:00
|
|
|
mhttp "go.unistack.org/micro-client-http/v3"
|
|
|
|
jsoncodec "go.unistack.org/micro-codec-json/v3"
|
|
|
|
rrouter "go.unistack.org/micro-router-register/v3"
|
|
|
|
pb "go.unistack.org/micro-tests/client/http/proto"
|
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/router"
|
2020-11-26 08:57:54 +03:00
|
|
|
)
|
|
|
|
|
2021-10-27 01:24:35 +03:00
|
|
|
var defaultHTTPCodecs = map[string]codec.Codec{
|
|
|
|
"application/json": jsoncodec.NewCodec(),
|
|
|
|
}
|
2020-11-26 08:57:54 +03:00
|
|
|
|
|
|
|
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{}
|
2021-07-05 16:26:57 +03:00
|
|
|
errMap := map[string]interface{}{"404": &pb.Error{}}
|
|
|
|
err := mhttp.GetError(c.Call(context.TODO(), req, rsp, mhttp.Method(http.MethodGet), mhttp.ErrorMap(errMap)))
|
2021-01-05 07:35:38 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("request must return non nil err")
|
|
|
|
}
|
2021-07-05 16:26:57 +03:00
|
|
|
|
|
|
|
if _, ok := err.(*pb.Error); !ok {
|
|
|
|
t.Fatalf("invalid response received: %T is not *pb.Error 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:34:04 +03:00
|
|
|
func TestNativeWithoutPath(t *testing.T) {
|
|
|
|
c := client.NewClientCallOptions(mhttp.NewClient(client.ContentType("application/json"), client.Codec("application/json", jsoncodec.NewCodec())), client.WithAddress("https://api.github.com/users"))
|
|
|
|
gh := pb.NewGithubClient("github", c)
|
|
|
|
|
|
|
|
rsp, err := gh.LookupUserWithoutPath(context.TODO(), &pb.LookupUserReq{Username: "vtolstov"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rsp.Name != "Vasiliy Tolstov" {
|
|
|
|
t.Fatalf("invalid rsp received: %#+v\n", rsp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2021-04-18 15:56:25 +03:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2021-07-16 00:41:10 +03:00
|
|
|
_, _ = w.Write(b)
|
2020-11-26 08:57:54 +03:00
|
|
|
})
|
2021-07-16 00:41:10 +03:00
|
|
|
go func() {
|
|
|
|
_ = http.Serve(l, mux)
|
|
|
|
}()
|
2020-11-26 08:57:54 +03:00
|
|
|
|
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
|
|
|
{
|
2021-04-29 22:51:46 +03:00
|
|
|
ID: "test.service.1",
|
2020-11-26 08:57:54 +03:00
|
|
|
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{
|
2022-01-24 22:44:26 +03:00
|
|
|
// Header: r.Header,
|
2020-11-26 08:57:54 +03:00
|
|
|
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
|
2021-07-16 00:41:10 +03:00
|
|
|
_ = rsp.Write(bufrw)
|
2020-11-26 08:57:54 +03:00
|
|
|
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
|
2021-07-16 00:41:10 +03:00
|
|
|
_ = rsp.Write(bufrw)
|
2020-11-26 08:57:54 +03:00
|
|
|
bufrw.Flush()
|
|
|
|
}
|
|
|
|
})
|
2021-07-16 00:41:10 +03:00
|
|
|
go func() {
|
|
|
|
_ = http.Serve(l, mux)
|
|
|
|
}()
|
2020-11-26 08:57:54 +03:00
|
|
|
|
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
|
|
|
{
|
2021-04-29 22:51:46 +03:00
|
|
|
ID: "test.service.1",
|
2020-11-26 08:57:54 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|