Fix body delivery bytes being incorrectly copied

このコミットが含まれているのは:
Cory Slep 2018-08-19 22:52:24 +02:00
コミット 387ed4a775
3個のファイルの変更55行の追加2行の削除

ファイルの表示

@ -1,5 +1,6 @@
v0.2.1 2018-08-19 v0.2.1 2018-08-19
* Request body is now correctly copied when sending federation messages.
* Change RWType and FollowResponse to bool and uint8, respectively. * Change RWType and FollowResponse to bool and uint8, respectively.
* Update README with applications using the go-fed/activity library. * Update README with applications using the go-fed/activity library.
* Update README with links to official implementation reports for go-fed. * Update README with links to official implementation reports for go-fed.

ファイルの表示

@ -6616,3 +6616,55 @@ func TestIssue75(t *testing.T) {
t.Fatalf("unexpected callback object: %s", err) t.Fatalf("unexpected callback object: %s", err)
} }
} }
func TestDelivery_Bytes(t *testing.T) {
app, socialApp, fedApp, socialCb, fedCb, d, httpClient, p := NewPubberTest(t)
PreparePubberPostOutboxTest(t, app, socialApp, fedApp, socialCb, fedCb, d, httpClient, p)
resp := httptest.NewRecorder()
req := Sign(ActivityPubRequest(httptest.NewRequest("POST", testOutboxURI, bytes.NewBuffer(MustSerialize(testCreateNote)))))
socialCb.create = func(c context.Context, s *streams.Create) error {
return nil
}
gotHttpDo := 0
var httpDeliveryRequest *http.Request
httpClient.do = func(req *http.Request) (*http.Response, error) {
gotHttpDo++
if gotHttpDo == 1 {
actorResp := &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBuffer(samActorJSON)),
}
return actorResp, nil
} else if gotHttpDo == 2 {
actorResp := &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBuffer(sallyActorJSON)),
}
return actorResp, nil
} else if gotHttpDo == 3 {
httpDeliveryRequest = req
okResp := &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBuffer([]byte{})),
}
return okResp, nil
}
return nil, nil
}
handled, err := p.PostOutbox(context.Background(), resp, req)
if err != nil {
t.Fatal(err)
} else if !handled {
t.Fatalf("expected handled, got !handled")
} else if gotHttpDo != 3 {
t.Fatalf("expected %d, got %d", 3, gotHttpDo)
} else if httpDeliveryRequest.Method != "POST" {
t.Fatalf("expected %s, got %s", "POST", httpDeliveryRequest.Method)
} else if deliveryBody, err := ioutil.ReadAll(httpDeliveryRequest.Body); err != nil {
t.Fatal(err)
} else if len(deliveryBody) == 0 {
t.Fatalf("empty delivery body")
} else if s := httpDeliveryRequest.URL.String(); s != samIRIInboxString {
t.Fatalf("expected %s, got %s", samIRIInboxString, s)
}
}

ファイルの表示

@ -167,8 +167,8 @@ func (f *federator) dereferenceAsUser(boxIRI, fetchIRI *url.URL) (obj vocab.Obje
// //
// creds is able to be nil. // creds is able to be nil.
func postToOutbox(c HttpClient, b []byte, to *url.URL, agent string, creds *creds, clock Clock) error { func postToOutbox(c HttpClient, b []byte, to *url.URL, agent string, creds *creds, clock Clock) error {
byteCopy := make([]byte, 0, len(b)) byteCopy := make([]byte, len(b))
copy(b, byteCopy) copy(byteCopy, b)
buf := bytes.NewBuffer(byteCopy) buf := bytes.NewBuffer(byteCopy)
req, err := http.NewRequest("POST", to.String(), buf) req, err := http.NewRequest("POST", to.String(), buf)
if err != nil { if err != nil {