Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --header/-H to dapr invoke #1287

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion cmd/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"net/http"
"os"
"runtime"
"strings"

"github.com/spf13/cobra"

Expand All @@ -34,6 +35,7 @@ var (
invokeVerb string
invokeDataFile string
invokeSocket string
invokeHeaders = make([]string, 0)
)

var InvokeCmd = &cobra.Command{
Expand All @@ -43,6 +45,9 @@ var InvokeCmd = &cobra.Command{
# Invoke a sample method on target app with POST Verb
dapr invoke --app-id target --method sample --data '{"key":"value"}

# Invoke a sample method on target app with customized Header
mukundansundar marked this conversation as resolved.
Show resolved Hide resolved
dapr invoke --app-id target --method sample --data '{"key":"value"} --header Customized-Header=Value

# Invoke a sample method on target app with GET Verb
dapr invoke --app-id target --method sample --verb GET

Expand Down Expand Up @@ -78,7 +83,25 @@ dapr invoke --unix-domain-socket --app-id target --method sample --verb GET
}
}

response, err := client.Invoke(invokeAppID, invokeAppMethod, bytePayload, invokeVerb, invokeSocket)
header := http.Header{}
for _, h := range invokeHeaders {
p := strings.Split(strings.TrimSpace(h), "=")
if len(p) != 2 {
print.FailureStatusEvent(os.Stderr, "Should one \"=\" in HTTP header.")
os.Exit(1)
}

if p[0] == "" {
print.FailureStatusEvent(os.Stderr, "A header name is required.")
os.Exit(1)
} else if p[1] == "" {
print.FailureStatusEvent(os.Stderr, "Value for header name is required.")
os.Exit(1)
}
header.Add(p[0], p[1])
}

response, err := client.Invoke(invokeAppID, invokeAppMethod, bytePayload, invokeVerb, header, invokeSocket)
if err != nil {
err = fmt.Errorf("error invoking app %s: %w", invokeAppID, err)
print.FailureStatusEvent(os.Stderr, err.Error())
Expand All @@ -98,6 +121,7 @@ func init() {
InvokeCmd.Flags().StringVarP(&invokeData, "data", "d", "", "The JSON serialized data string (optional)")
InvokeCmd.Flags().StringVarP(&invokeVerb, "verb", "v", defaultHTTPVerb, "The HTTP verb to use")
InvokeCmd.Flags().StringVarP(&invokeDataFile, "data-file", "f", "", "A file containing the JSON serialized data (optional)")
InvokeCmd.Flags().StringArrayVarP(&invokeHeaders, "header", "H", []string{}, "0 or more HTTP Header")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
InvokeCmd.Flags().StringArrayVarP(&invokeHeaders, "header", "H", []string{}, "0 or more HTTP Header")
InvokeCmd.Flags().StringArrayVarP(&invokeHeaders, "header", "H", []string{}, "HTTP headers to be used on invoke")

InvokeCmd.Flags().BoolP("help", "h", false, "Print this help message")
InvokeCmd.Flags().StringVarP(&invokeSocket, "unix-domain-socket", "u", "", "Path to a unix domain socket dir. If specified, Dapr API servers will use Unix Domain Sockets")
InvokeCmd.MarkFlagRequired("app-id")
Expand Down
4 changes: 3 additions & 1 deletion pkg/standalone/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ limitations under the License.

package standalone

import "net/http"

type DaprProcess interface {
List() ([]ListOutput, error)
}
Expand All @@ -22,7 +24,7 @@ type daprProcess struct{}
// Client is the interface the wraps all the methods exposed by the Dapr CLI.
type Client interface {
// Invoke is a command to invoke a remote or local dapr instance.
Invoke(appID, method string, data []byte, verb string, socket string) (string, error)
Invoke(appID, method string, data []byte, verb string, header http.Header, socket string) (string, error)
// Publish is used to publish event to a topic in a pubsub for an app ID.
Publish(publishAppID, pubsubName, topic string, payload []byte, socket string, metadata map[string]interface{}) error
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/standalone/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Invoke is a command to invoke a remote or local dapr instance.
func (s *Standalone) Invoke(appID, method string, data []byte, verb string, path string) (string, error) {
func (s *Standalone) Invoke(appID, method string, data []byte, verb string, header http.Header, path string) (string, error) {
list, err := s.process.List()
if err != nil {
return "", err
Expand All @@ -39,7 +39,13 @@ func (s *Standalone) Invoke(appID, method string, data []byte, verb string, path
if err != nil {
return "", err
}

req.Header.Set("Content-Type", "application/json")
for h, vs := range header {
for _, v := range vs {
req.Header.Add(h, v)
}
}

var httpc http.Client

Expand Down
28 changes: 24 additions & 4 deletions pkg/standalone/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package standalone

import (
"fmt"
"net/http"
"os"
"runtime"
"testing"
Expand All @@ -35,13 +36,15 @@ func TestInvoke(t *testing.T) {
listErr error
expectedPath string
postResponse string
header http.Header
resp string
}{
{
name: "list apps error",
errorExpected: true,
errString: assert.AnError.Error(),
listErr: assert.AnError,
header: nil,
},
{
name: "appID not found",
Expand All @@ -51,6 +54,7 @@ func TestInvoke(t *testing.T) {
lo: ListOutput{
AppID: "testapp",
},
header: nil,
},
{
name: "appID found successful invoke empty response",
Expand All @@ -59,6 +63,7 @@ func TestInvoke(t *testing.T) {
lo: ListOutput{
AppID: "testapp",
},
header: nil,
},
{
name: "appID found successful invoke",
Expand All @@ -69,8 +74,23 @@ func TestInvoke(t *testing.T) {
},
expectedPath: "/v1.0/invoke/testapp/method/test",
postResponse: "test payload",
header: nil,
resp: "successful invoke",
},
{
name: "appID found successful invoke with customized header",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add one test with multiple header values.

appID: "testapp",
method: "test",
lo: ListOutput{
AppID: "testapp",
},
expectedPath: "/v1.0/invoke/testapp/method/test",
postResponse: "test payload",
resp: "successful invoke",
header: http.Header{
"Customized-Header": []string{"Value"},
},
},
}

for _, socket := range []string{"", "/tmp"} {
Expand Down Expand Up @@ -105,7 +125,7 @@ func TestInvoke(t *testing.T) {
},
}

res, err := client.Invoke(tc.appID, tc.method, []byte(tc.resp), "GET", socket)
res, err := client.Invoke(tc.appID, tc.method, []byte(tc.resp), "GET", tc.header, socket)
if tc.errorExpected {
assert.Error(t, err, "expected an error")
assert.Equal(t, tc.errString, err.Error(), "expected error strings to match")
Expand Down Expand Up @@ -137,7 +157,7 @@ func TestInvoke(t *testing.T) {
Err: tc.listErr,
},
}
res, err := client.Invoke(tc.appID, tc.method, []byte(tc.resp), "POST", socket)
res, err := client.Invoke(tc.appID, tc.method, []byte(tc.resp), "POST", tc.header, socket)
if tc.errorExpected {
assert.Error(t, err, "expected an error")
assert.Equal(t, tc.errString, err.Error(), "expected error strings to match")
Expand Down Expand Up @@ -169,7 +189,7 @@ func TestInvoke(t *testing.T) {
Err: tc.listErr,
},
}
res, err := client.Invoke(tc.appID, tc.method, []byte(tc.resp), "DELETE", socket)
res, err := client.Invoke(tc.appID, tc.method, []byte(tc.resp), "DELETE", tc.header, socket)
if tc.errorExpected {
assert.Error(t, err, "expected an error")
assert.Equal(t, tc.errString, err.Error(), "expected error strings to match")
Expand Down Expand Up @@ -202,7 +222,7 @@ func TestInvoke(t *testing.T) {
Err: tc.listErr,
},
}
res, err := client.Invoke(tc.appID, tc.method, []byte(tc.resp), "PUT", socket)
res, err := client.Invoke(tc.appID, tc.method, []byte(tc.resp), "PUT", tc.header, socket)
if tc.errorExpected {
assert.Error(t, err, "expected an error")
assert.Equal(t, tc.errString, err.Error(), "expected error strings to match")
Expand Down