mirror of https://github.com/go-gitea/gitea.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.3 KiB
45 lines
1.3 KiB
// Copyright 2022 The Gitea Authors. All rights reserved. |
|
// SPDX-License-Identifier: MIT |
|
|
|
package activitypub |
|
|
|
import ( |
|
"fmt" |
|
"io" |
|
"net/http" |
|
"net/http/httptest" |
|
"testing" |
|
|
|
"code.gitea.io/gitea/models/unittest" |
|
user_model "code.gitea.io/gitea/models/user" |
|
"code.gitea.io/gitea/modules/setting" |
|
|
|
"github.com/stretchr/testify/assert" |
|
) |
|
|
|
func TestActivityPubSignedPost(t *testing.T) { |
|
assert.NoError(t, unittest.PrepareTestDatabase()) |
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) |
|
pubID := "https://example.com/pubID" |
|
c, err := NewClient(t.Context(), user, pubID) |
|
assert.NoError(t, err) |
|
|
|
expected := "BODY" |
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
assert.Regexp(t, "^"+setting.Federation.DigestAlgorithm, r.Header.Get("Digest")) |
|
assert.Contains(t, r.Header.Get("Signature"), pubID) |
|
assert.Equal(t, ActivityStreamsContentType, r.Header.Get("Content-Type")) |
|
body, err := io.ReadAll(r.Body) |
|
assert.NoError(t, err) |
|
assert.Equal(t, expected, string(body)) |
|
fmt.Fprint(w, expected) |
|
})) |
|
defer srv.Close() |
|
|
|
r, err := c.Post([]byte(expected), srv.URL) |
|
assert.NoError(t, err) |
|
defer r.Body.Close() |
|
body, err := io.ReadAll(r.Body) |
|
assert.NoError(t, err) |
|
assert.Equal(t, expected, string(body)) |
|
}
|
|
|