Skip to content

Commit

Permalink
Merge upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
jsccast committed Apr 9, 2021
2 parents 3afa2f8 + 9272c11 commit 9176efc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions chans/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func extractHTTPRequest(ctx *dsl.Ctx, m dsl.Msg) (*HTTPRequest, error) {

if req.Body != nil {
real.Body = ioutil.NopCloser(bytes.NewReader(req.body))
real.ContentLength = int64(len(req.body))
}

req.req = real
Expand Down
61 changes: 61 additions & 0 deletions chans/httpclient/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -150,3 +151,63 @@ func TestHTTPRequestPolling(t *testing.T) {
t.Fatal("received another request")
}
}

func TestContentLength(t *testing.T) {
var (
ctx = dsl.NewCtx(context.Background())
interval = 50 * time.Millisecond // PollInterval

// Four is the only number that has the same number of
// letters as its value. Fun fact.
body = "four"

ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if int(r.ContentLength) != len(body) {
bs, _ := ioutil.ReadAll(r.Body)
t.Fatalf("%d: %s", r.ContentLength, bs)
}
fmt.Fprintln(w, "{}")
}))
)

defer ts.Close()

c, err := NewHTTPClientChan(ctx, &HTTPClientOpts{})
if err != nil {
t.Fatal(err)
}

if err = c.Open(ctx); err != nil {
t.Fatal(err)
}

defer func() {
if err := c.Close(ctx); err != nil {
t.Fatal(err)
}
}()

payload, err := json.Marshal(&HTTPRequest{
Method: "POST",
URL: ts.URL,
HTTPRequestCtl: HTTPRequestCtl{
PollInterval: interval.String(),
},
Body: body,
RequestBodySerialization: "string",
})

if err != nil {
t.Fatal(err)
}

err = c.Pub(ctx, dsl.Msg{
Payload: string(payload),
})

if err != nil {
t.Fatal(err)
}

<-c.Recv(ctx)
}

0 comments on commit 9176efc

Please sign in to comment.