2022-02-06 07:06:32 +00:00
|
|
|
// Copyright (C) 2017 Michael J. Fromberger. All Rights Reserved.
|
|
|
|
|
2021-12-04 16:42:11 +00:00
|
|
|
package jhttp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
|
2022-02-06 07:06:32 +00:00
|
|
|
"github.com/creachadair/jrpc2"
|
2021-12-04 16:42:11 +00:00
|
|
|
"github.com/creachadair/jrpc2/handler"
|
|
|
|
"github.com/creachadair/jrpc2/jhttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Example() {
|
2022-02-06 07:06:32 +00:00
|
|
|
// Set up a bridge exporting a simple service.
|
2021-12-04 16:42:11 +00:00
|
|
|
b := jhttp.NewBridge(handler.Map{
|
2022-02-06 07:06:32 +00:00
|
|
|
"Test": handler.New(func(ctx context.Context, ss []string) string {
|
|
|
|
return strings.Join(ss, " ")
|
2021-12-04 16:42:11 +00:00
|
|
|
}),
|
|
|
|
}, nil)
|
|
|
|
defer b.Close()
|
|
|
|
|
2022-02-06 07:06:32 +00:00
|
|
|
// The bridge can be used as the handler for an HTTP server.
|
2021-12-04 16:42:11 +00:00
|
|
|
hsrv := httptest.NewServer(b)
|
|
|
|
defer hsrv.Close()
|
|
|
|
|
2022-02-06 07:06:32 +00:00
|
|
|
// Set up a client using an HTTP channel, and use it to call the test
|
|
|
|
// service exported by the bridge.
|
|
|
|
ch := jhttp.NewChannel(hsrv.URL, nil)
|
|
|
|
cli := jrpc2.NewClient(ch, nil)
|
|
|
|
|
|
|
|
var result string
|
|
|
|
if err := cli.CallResult(context.Background(), "Test", []string{
|
|
|
|
"full", "plate", "and", "packing", "steel",
|
|
|
|
}, &result); err != nil {
|
|
|
|
log.Fatalf("Call failed: %v", err)
|
2021-12-04 16:42:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-06 07:06:32 +00:00
|
|
|
fmt.Println("Result:", result)
|
2021-12-04 16:42:11 +00:00
|
|
|
// Output:
|
2022-02-06 07:06:32 +00:00
|
|
|
// Result: full plate and packing steel
|
2021-12-04 16:42:11 +00:00
|
|
|
}
|