code.gitea.io/gitea@v1.19.3/modules/activitypub/client_test.go (about) 1 // Copyright 2022 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package activitypub 5 6 import ( 7 "fmt" 8 "io" 9 "net/http" 10 "net/http/httptest" 11 "regexp" 12 "testing" 13 14 "code.gitea.io/gitea/models/unittest" 15 user_model "code.gitea.io/gitea/models/user" 16 "code.gitea.io/gitea/modules/setting" 17 18 _ "code.gitea.io/gitea/models" // https://discourse.gitea.io/t/testfixtures-could-not-clean-table-access-no-such-table-access/4137/4 19 20 "github.com/stretchr/testify/assert" 21 ) 22 23 func TestActivityPubSignedPost(t *testing.T) { 24 assert.NoError(t, unittest.PrepareTestDatabase()) 25 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) 26 pubID := "https://example.com/pubID" 27 c, err := NewClient(user, pubID) 28 assert.NoError(t, err) 29 30 expected := "BODY" 31 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 32 assert.Regexp(t, regexp.MustCompile("^"+setting.Federation.DigestAlgorithm), r.Header.Get("Digest")) 33 assert.Contains(t, r.Header.Get("Signature"), pubID) 34 assert.Equal(t, r.Header.Get("Content-Type"), ActivityStreamsContentType) 35 body, err := io.ReadAll(r.Body) 36 assert.NoError(t, err) 37 assert.Equal(t, expected, string(body)) 38 fmt.Fprint(w, expected) 39 })) 40 defer srv.Close() 41 42 r, err := c.Post([]byte(expected), srv.URL) 43 assert.NoError(t, err) 44 defer r.Body.Close() 45 body, err := io.ReadAll(r.Body) 46 assert.NoError(t, err) 47 assert.Equal(t, expected, string(body)) 48 }