Skip to content

Commit 387ed4a

Browse files
committed
Fix body delivery bytes being incorrectly copied
1 parent 17fce22 commit 387ed4a

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
v0.2.1 2018-08-19
22

3+
* Request body is now correctly copied when sending federation messages.
34
* Change RWType and FollowResponse to bool and uint8, respectively.
45
* Update README with applications using the go-fed/activity library.
56
* Update README with links to official implementation reports for go-fed.

pub/fed_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6616,3 +6616,55 @@ func TestIssue75(t *testing.T) {
66166616
t.Fatalf("unexpected callback object: %s", err)
66176617
}
66186618
}
6619+
6620+
func TestDelivery_Bytes(t *testing.T) {
6621+
app, socialApp, fedApp, socialCb, fedCb, d, httpClient, p := NewPubberTest(t)
6622+
PreparePubberPostOutboxTest(t, app, socialApp, fedApp, socialCb, fedCb, d, httpClient, p)
6623+
resp := httptest.NewRecorder()
6624+
req := Sign(ActivityPubRequest(httptest.NewRequest("POST", testOutboxURI, bytes.NewBuffer(MustSerialize(testCreateNote)))))
6625+
socialCb.create = func(c context.Context, s *streams.Create) error {
6626+
return nil
6627+
}
6628+
gotHttpDo := 0
6629+
var httpDeliveryRequest *http.Request
6630+
httpClient.do = func(req *http.Request) (*http.Response, error) {
6631+
gotHttpDo++
6632+
if gotHttpDo == 1 {
6633+
actorResp := &http.Response{
6634+
StatusCode: http.StatusOK,
6635+
Body: ioutil.NopCloser(bytes.NewBuffer(samActorJSON)),
6636+
}
6637+
return actorResp, nil
6638+
} else if gotHttpDo == 2 {
6639+
actorResp := &http.Response{
6640+
StatusCode: http.StatusOK,
6641+
Body: ioutil.NopCloser(bytes.NewBuffer(sallyActorJSON)),
6642+
}
6643+
return actorResp, nil
6644+
} else if gotHttpDo == 3 {
6645+
httpDeliveryRequest = req
6646+
okResp := &http.Response{
6647+
StatusCode: http.StatusOK,
6648+
Body: ioutil.NopCloser(bytes.NewBuffer([]byte{})),
6649+
}
6650+
return okResp, nil
6651+
}
6652+
return nil, nil
6653+
}
6654+
handled, err := p.PostOutbox(context.Background(), resp, req)
6655+
if err != nil {
6656+
t.Fatal(err)
6657+
} else if !handled {
6658+
t.Fatalf("expected handled, got !handled")
6659+
} else if gotHttpDo != 3 {
6660+
t.Fatalf("expected %d, got %d", 3, gotHttpDo)
6661+
} else if httpDeliveryRequest.Method != "POST" {
6662+
t.Fatalf("expected %s, got %s", "POST", httpDeliveryRequest.Method)
6663+
} else if deliveryBody, err := ioutil.ReadAll(httpDeliveryRequest.Body); err != nil {
6664+
t.Fatal(err)
6665+
} else if len(deliveryBody) == 0 {
6666+
t.Fatalf("empty delivery body")
6667+
} else if s := httpDeliveryRequest.URL.String(); s != samIRIInboxString {
6668+
t.Fatalf("expected %s, got %s", samIRIInboxString, s)
6669+
}
6670+
}

pub/internal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ func (f *federator) dereferenceAsUser(boxIRI, fetchIRI *url.URL) (obj vocab.Obje
167167
//
168168
// creds is able to be nil.
169169
func postToOutbox(c HttpClient, b []byte, to *url.URL, agent string, creds *creds, clock Clock) error {
170-
byteCopy := make([]byte, 0, len(b))
171-
copy(b, byteCopy)
170+
byteCopy := make([]byte, len(b))
171+
copy(byteCopy, b)
172172
buf := bytes.NewBuffer(byteCopy)
173173
req, err := http.NewRequest("POST", to.String(), buf)
174174
if err != nil {

0 commit comments

Comments
 (0)