fix up tests to use main mock httpclient

This commit is contained in:
tsmethurst 2022-06-02 12:38:39 +02:00
parent 181f782d13
commit 49490d8999
9 changed files with 98 additions and 191 deletions

View file

@ -128,7 +128,7 @@ func (suite *StatusCreateTestSuite) TestMentionUnknownAccount() {
ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080/%s", status.BasePath), nil) // the endpoint we're hitting
ctx.Request.Header.Set("accept", "application/json")
ctx.Request.Form = url.Values{
"status": {"hello @foss_satan@fossbros-anonymous.io"},
"status": {"hello @brand_new_person@unknown-instance.com"},
"visibility": {string(model.VisibilityPublic)},
}
suite.statusModule.StatusCreatePOSTHandler(ctx)
@ -145,7 +145,7 @@ func (suite *StatusCreateTestSuite) TestMentionUnknownAccount() {
suite.NoError(err)
// if the status is properly formatted, that means the account has been put in the db
suite.Equal("<p>hello <span class=\"h-card\"><a href=\"http://fossbros-anonymous.io/@foss_satan\" class=\"u-url mention\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">@<span>foss_satan</span></a></span></p>", statusReply.Content)
suite.Equal(`<p>hello <span class="h-card"><a href="https://unknown-instance.com/@brand_new_person" class="u-url mention" rel="nofollow noreferrer noopener" target="_blank">@<span>brand_new_person</span></a></span></p>`, statusReply.Content)
suite.Equal(model.VisibilityPublic, statusReply.Visibility)
}

View file

@ -19,10 +19,7 @@
package federation_test
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/url"
"testing"
"time"
@ -60,15 +57,9 @@ func (suite *FederatingActorTestSuite) TestSendNoRemoteFollowers() {
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
// setup transport controller with a no-op client so we don't make external calls
sentMessages := []*url.URL{}
tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(func(req *http.Request) (*http.Response, error) {
sentMessages = append(sentMessages, req.URL)
r := ioutil.NopCloser(bytes.NewReader([]byte{}))
return &http.Response{
StatusCode: 200,
Body: r,
}, nil
}, ""), suite.db, fedWorker)
httpClient := testrig.NewMockHTTPClient(nil, "../../testrig/media")
tc := testrig.NewTestTransportController(httpClient, suite.db, fedWorker)
// setup module being tested
federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.tc, testrig.NewTestMediaManager(suite.db, suite.storage))
@ -77,7 +68,7 @@ func (suite *FederatingActorTestSuite) TestSendNoRemoteFollowers() {
suite.NotNil(activity)
// because zork has no remote followers, sent messages should be empty (no messages sent to own instance)
suite.Empty(sentMessages)
suite.Empty(httpClient.SentMessages)
}
func (suite *FederatingActorTestSuite) TestSendRemoteFollower() {
@ -87,8 +78,8 @@ func (suite *FederatingActorTestSuite) TestSendRemoteFollower() {
err := suite.db.Put(ctx, &gtsmodel.Follow{
ID: "01G1TRWV4AYCDBX5HRWT2EVBCV",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
CreatedAt: testrig.TimeMustParse("2022-06-02T12:22:21+02:00"),
UpdatedAt: testrig.TimeMustParse("2022-06-02T12:22:21+02:00"),
AccountID: testRemoteAccount.ID,
TargetAccountID: testAccount.ID,
ShowReblogs: true,
@ -100,7 +91,7 @@ func (suite *FederatingActorTestSuite) TestSendRemoteFollower() {
testNote := testrig.NewAPNote(
testrig.URLMustParse("http://localhost:8080/users/the_mighty_zork/statuses/01G1TR6BADACCZWQMNF9X21TV5"),
testrig.URLMustParse("http://localhost:8080/@the_mighty_zork/statuses/01G1TR6BADACCZWQMNF9X21TV5"),
time.Now(),
testrig.TimeMustParse("2022-06-02T12:22:21+02:00"),
"boobies",
"",
testrig.URLMustParse(testAccount.URI),
@ -110,20 +101,12 @@ func (suite *FederatingActorTestSuite) TestSendRemoteFollower() {
nil,
nil,
)
testActivity := testrig.WrapAPNoteInCreate(testrig.URLMustParse("http://localhost:8080/whatever_some_create"), testrig.URLMustParse(testAccount.URI), time.Now(), testNote)
testActivity := testrig.WrapAPNoteInCreate(testrig.URLMustParse("http://localhost:8080/whatever_some_create"), testrig.URLMustParse(testAccount.URI), testrig.TimeMustParse("2022-06-02T12:22:21+02:00"), testNote)
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
// setup transport controller with a no-op client so we don't make external calls
sentMessages := []*url.URL{}
tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(func(req *http.Request) (*http.Response, error) {
sentMessages = append(sentMessages, req.URL)
r := ioutil.NopCloser(bytes.NewReader([]byte{}))
return &http.Response{
StatusCode: 200,
Body: r,
}, nil
}, ""), suite.db, fedWorker)
httpClient := testrig.NewMockHTTPClient(nil, "../../testrig/media")
tc := testrig.NewTestTransportController(httpClient, suite.db, fedWorker)
// setup module being tested
federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.tc, testrig.NewTestMediaManager(suite.db, suite.storage))
@ -132,8 +115,10 @@ func (suite *FederatingActorTestSuite) TestSendRemoteFollower() {
suite.NotNil(activity)
// because we added 1 remote follower for zork, there should be a url in sentMessage
suite.Len(sentMessages, 1)
suite.Equal(testRemoteAccount.InboxURI, sentMessages[0].String())
suite.Len(httpClient.SentMessages, 1)
msg, ok := httpClient.SentMessages[testRemoteAccount.InboxURI]
suite.True(ok)
suite.Equal(`{"@context":"https://www.w3.org/ns/activitystreams","actor":"http://localhost:8080/users/the_mighty_zork","id":"http://localhost:8080/whatever_some_create","object":{"attributedTo":"http://localhost:8080/users/the_mighty_zork","content":"boobies","id":"http://localhost:8080/users/the_mighty_zork/statuses/01G1TR6BADACCZWQMNF9X21TV5","published":"2022-06-02T12:22:21+02:00","tag":[],"to":"http://localhost:8080/users/the_mighty_zork/followers","type":"Note","url":"http://localhost:8080/@the_mighty_zork/statuses/01G1TR6BADACCZWQMNF9X21TV5"},"published":"2022-06-02T12:22:21+02:00","to":"http://localhost:8080/users/the_mighty_zork/followers","type":"Create"}`, string(msg))
}
func TestFederatingActorTestSuite(t *testing.T) {

View file

@ -45,8 +45,8 @@ func (suite *FederatingProtocolTestSuite) TestPostInboxRequestBodyHook1() {
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
// setup transport controller with a no-op client so we don't make external calls
tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, ""), suite.db, fedWorker)
httpClient := testrig.NewMockHTTPClient(nil, "../../testrig/media")
tc := testrig.NewTestTransportController(httpClient, suite.db, fedWorker)
// setup module being tested
federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.tc, testrig.NewTestMediaManager(suite.db, suite.storage))
@ -76,8 +76,8 @@ func (suite *FederatingProtocolTestSuite) TestPostInboxRequestBodyHook2() {
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
// setup transport controller with a no-op client so we don't make external calls
tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, ""), suite.db, fedWorker)
httpClient := testrig.NewMockHTTPClient(nil, "../../testrig/media")
tc := testrig.NewTestTransportController(httpClient, suite.db, fedWorker)
// setup module being tested
federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.tc, testrig.NewTestMediaManager(suite.db, suite.storage))
@ -109,8 +109,8 @@ func (suite *FederatingProtocolTestSuite) TestPostInboxRequestBodyHook3() {
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
// setup transport controller with a no-op client so we don't make external calls
tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, ""), suite.db, fedWorker)
httpClient := testrig.NewMockHTTPClient(nil, "../../testrig/media")
tc := testrig.NewTestTransportController(httpClient, suite.db, fedWorker)
// setup module being tested
federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.tc, testrig.NewTestMediaManager(suite.db, suite.storage))
@ -144,7 +144,8 @@ func (suite *FederatingProtocolTestSuite) TestAuthenticatePostInbox() {
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, ""), suite.db, fedWorker)
httpClient := testrig.NewMockHTTPClient(nil, "../../testrig/media")
tc := testrig.NewTestTransportController(httpClient, suite.db, fedWorker)
// now setup module being tested, with the mock transport controller
federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.tc, testrig.NewTestMediaManager(suite.db, suite.storage))
@ -183,7 +184,8 @@ func (suite *FederatingProtocolTestSuite) TestAuthenticatePostInbox() {
func (suite *FederatingProtocolTestSuite) TestBlocked1() {
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, ""), suite.db, fedWorker)
httpClient := testrig.NewMockHTTPClient(nil, "../../testrig/media")
tc := testrig.NewTestTransportController(httpClient, suite.db, fedWorker)
federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.tc, testrig.NewTestMediaManager(suite.db, suite.storage))
sendingAccount := suite.testAccounts["remote_account_1"]
@ -205,7 +207,8 @@ func (suite *FederatingProtocolTestSuite) TestBlocked1() {
func (suite *FederatingProtocolTestSuite) TestBlocked2() {
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, ""), suite.db, fedWorker)
httpClient := testrig.NewMockHTTPClient(nil, "../../testrig/media")
tc := testrig.NewTestTransportController(httpClient, suite.db, fedWorker)
federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.tc, testrig.NewTestMediaManager(suite.db, suite.storage))
sendingAccount := suite.testAccounts["remote_account_1"]
@ -238,7 +241,8 @@ func (suite *FederatingProtocolTestSuite) TestBlocked2() {
func (suite *FederatingProtocolTestSuite) TestBlocked3() {
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, ""), suite.db, fedWorker)
httpClient := testrig.NewMockHTTPClient(nil, "../../testrig/media")
tc := testrig.NewTestTransportController(httpClient, suite.db, fedWorker)
federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.tc, testrig.NewTestMediaManager(suite.db, suite.storage))
sendingAccount := suite.testAccounts["remote_account_1"]
@ -274,7 +278,8 @@ func (suite *FederatingProtocolTestSuite) TestBlocked3() {
func (suite *FederatingProtocolTestSuite) TestBlocked4() {
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, ""), suite.db, fedWorker)
httpClient := testrig.NewMockHTTPClient(nil, "../../testrig/media")
tc := testrig.NewTestTransportController(httpClient, suite.db, fedWorker)
federator := federation.NewFederator(suite.db, testrig.NewTestFederatingDB(suite.db, fedWorker), tc, suite.tc, testrig.NewTestMediaManager(suite.db, suite.storage))
sendingAccount := suite.testAccounts["remote_account_1"]

View file

@ -60,7 +60,7 @@ func (suite *AccountTestSuite) TestAccountDeleteLocal() {
time.Sleep(1 * time.Second) // wait a sec for the delete to process
// the delete should be federated outwards to the following account's inbox
sent, ok := suite.sentHTTPRequests[followingAccount.InboxURI]
sent, ok := suite.httpClient.SentMessages[followingAccount.InboxURI]
suite.True(ok)
delete := &struct {
Actor string `json:"actor"`

View file

@ -57,7 +57,7 @@ func (suite *FollowRequestTestSuite) TestFollowRequestAccept() {
time.Sleep(1 * time.Second)
// accept should be sent to some_user
sent, ok := suite.sentHTTPRequests[requestingAccount.InboxURI]
sent, ok := suite.httpClient.SentMessages[requestingAccount.InboxURI]
suite.True(ok)
accept := &struct {
@ -109,7 +109,7 @@ func (suite *FollowRequestTestSuite) TestFollowRequestReject() {
time.Sleep(1 * time.Second)
// reject should be sent to some_user
sent, ok := suite.sentHTTPRequests[requestingAccount.InboxURI]
sent, ok := suite.httpClient.SentMessages[requestingAccount.InboxURI]
suite.True(ok)
reject := &struct {

View file

@ -423,7 +423,7 @@ func (suite *FromFederatorTestSuite) TestProcessFollowRequestLocked() {
suite.Equal(originAccount.ID, notif.Account.ID)
// no messages should have been sent out, since we didn't need to federate an accept
suite.Empty(suite.sentHTTPRequests)
suite.Empty(suite.httpClient.SentMessages)
}
func (suite *FromFederatorTestSuite) TestProcessFollowRequestUnlocked() {
@ -474,8 +474,8 @@ func (suite *FromFederatorTestSuite) TestProcessFollowRequestUnlocked() {
suite.Equal(originAccount.ID, notif.Account.ID)
// an accept message should be sent to satan's inbox
suite.Len(suite.sentHTTPRequests, 1)
acceptBytes := suite.sentHTTPRequests[originAccount.InboxURI]
suite.Len(suite.httpClient.SentMessages, 1)
acceptBytes := suite.httpClient.SentMessages[originAccount.InboxURI]
accept := &struct {
Actor string `json:"actor"`
ID string `json:"id"`

View file

@ -19,17 +19,8 @@
package processing_test
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
"codeberg.org/gruf/go-store/kv"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/activity/streams"
"github.com/superseriousbusiness/gotosocial/internal/concurrency"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/email"
@ -51,6 +42,7 @@ type ProcessingStandardTestSuite struct {
storage *kv.KVStore
mediaManager media.Manager
typeconverter typeutils.TypeConverter
httpClient *testrig.MockHTTPClient
transportController transport.Controller
federator federation.Federator
oauthServer oauth.Server
@ -70,8 +62,6 @@ type ProcessingStandardTestSuite struct {
testBlocks map[string]*gtsmodel.Block
testActivities map[string]testrig.ActivityWithSignature
sentHTTPRequests map[string][]byte
processor processing.Processor
}
@ -103,139 +93,12 @@ func (suite *ProcessingStandardTestSuite) SetupTest() {
suite.testActivities = testrig.NewTestActivities(suite.testAccounts)
suite.storage = testrig.NewTestStorage()
suite.typeconverter = testrig.NewTestTypeConverter(suite.db)
// make an http client that stores POST requests it receives into a map,
// and also responds to correctly to dereference requests
suite.sentHTTPRequests = make(map[string][]byte)
httpClient := testrig.NewMockHTTPClient(func(req *http.Request) (*http.Response, error) {
if req.Method == http.MethodPost && req.Body != nil {
requestBytes, err := ioutil.ReadAll(req.Body)
if err != nil {
panic(err)
}
if err := req.Body.Close(); err != nil {
panic(err)
}
suite.sentHTTPRequests[req.URL.String()] = requestBytes
}
if strings.Contains(req.URL.String(), ".well-known/webfinger") {
responseCode, responseBytes, responseContentType, responseContentLength := testrig.WebfingerResponse(req)
reader := bytes.NewReader(responseBytes)
readCloser := io.NopCloser(reader)
response := &http.Response{
StatusCode: responseCode,
Body: readCloser,
ContentLength: int64(responseContentLength),
Header: http.Header{
"content-type": {responseContentType},
},
}
return response, nil
}
if req.URL.String() == suite.testAccounts["remote_account_1"].URI {
// the request is for remote account 1
satan := suite.testAccounts["remote_account_1"]
satanAS, err := suite.typeconverter.AccountToAS(context.Background(), satan)
if err != nil {
panic(err)
}
satanI, err := streams.Serialize(satanAS)
if err != nil {
panic(err)
}
satanJson, err := json.Marshal(satanI)
if err != nil {
panic(err)
}
responseType := "application/activity+json"
reader := bytes.NewReader(satanJson)
readCloser := io.NopCloser(reader)
response := &http.Response{
StatusCode: 200,
Body: readCloser,
ContentLength: int64(len(satanJson)),
Header: http.Header{
"content-type": {responseType},
},
}
return response, nil
}
if req.URL.String() == suite.testAccounts["remote_account_2"].URI {
// the request is for remote account 2
someAccount := suite.testAccounts["remote_account_2"]
someAccountAS, err := suite.typeconverter.AccountToAS(context.Background(), someAccount)
if err != nil {
panic(err)
}
someAccountI, err := streams.Serialize(someAccountAS)
if err != nil {
panic(err)
}
someAccountJson, err := json.Marshal(someAccountI)
if err != nil {
panic(err)
}
responseType := "application/activity+json"
reader := bytes.NewReader(someAccountJson)
readCloser := io.NopCloser(reader)
response := &http.Response{
StatusCode: 200,
Body: readCloser,
ContentLength: int64(len(someAccountJson)),
Header: http.Header{
"content-type": {responseType},
},
}
return response, nil
}
if req.URL.String() == "http://example.org/users/some_user/statuses/afaba698-5740-4e32-a702-af61aa543bc1" {
// the request is for the forwarded message
message := suite.testActivities["forwarded_message"].Activity.GetActivityStreamsObject().At(0).GetActivityStreamsNote()
messageI, err := streams.Serialize(message)
if err != nil {
panic(err)
}
messageJson, err := json.Marshal(messageI)
if err != nil {
panic(err)
}
responseType := "application/activity+json"
reader := bytes.NewReader(messageJson)
readCloser := io.NopCloser(reader)
response := &http.Response{
StatusCode: 200,
Body: readCloser,
ContentLength: int64(len(messageJson)),
Header: http.Header{
"content-type": {responseType},
},
}
return response, nil
}
r := ioutil.NopCloser(bytes.NewReader([]byte{}))
return &http.Response{
StatusCode: 200,
Body: r,
}, nil
}, "")
suite.httpClient = testrig.NewMockHTTPClient(nil, "../../testrig/media")
clientWorker := concurrency.NewWorkerPool[messages.FromClientAPI](-1, -1)
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
suite.transportController = testrig.NewTestTransportController(httpClient, suite.db, fedWorker)
suite.transportController = testrig.NewTestTransportController(suite.httpClient, suite.db, fedWorker)
suite.mediaManager = testrig.NewTestMediaManager(suite.db, suite.storage)
suite.federator = testrig.NewTestFederator(suite.db, suite.transportController, suite.storage, suite.mediaManager, fedWorker)
suite.oauthServer = testrig.NewTestOauthServer(suite.db)

View file

@ -1787,6 +1787,26 @@ func NewTestFediPeople() map[string]vocab.ActivityStreamsPerson {
"image/png",
false,
),
" ://unknown-instance.com/users/brand_new_person": newAPPerson(
URLMustParse("https://unknown-instance.com/users/brand_new_person"),
URLMustParse("https://unknown-instance.com/users/brand_new_person/following"),
URLMustParse("https://unknown-instance.com/users/brand_new_person/followers"),
URLMustParse("https://unknown-instance.com/users/brand_new_person/inbox"),
URLMustParse("https://unknown-instance.com/users/brand_new_person/outbox"),
URLMustParse("https://unknown-instance.com/users/brand_new_person/collections/featured"),
"brand_new_person",
"Geoff Brando New Personson",
"hey I'm a new person, your instance hasn't seen me yet uwu",
URLMustParse("https://unknown-instance.com/@brand_new_person"),
true,
URLMustParse("https://unknown-instance.com/users/brand_new_person#main-key"),
newPerson1Pub,
nil,
"image/jpeg",
nil,
"image/png",
false,
),
}
}
@ -1892,6 +1912,25 @@ func NewTestFediAttachments(relativePath string) map[string]RemoteAttachmentFile
func NewTestFediStatuses() map[string]vocab.ActivityStreamsNote {
return map[string]vocab.ActivityStreamsNote{
"http://example.org/users/some_user/statuses/afaba698-5740-4e32-a702-af61aa543bc1": NewAPNote(
URLMustParse("http://example.org/users/some_user/statuses/afaba698-5740-4e32-a702-af61aa543bc1"),
URLMustParse("http://example.org/@some_user/afaba698-5740-4e32-a702-af61aa543bc1"),
time.Now(),
"this is a public status, please forward it!",
"",
URLMustParse("http://example.org/users/some_user"),
[]*url.URL{URLMustParse(pub.PublicActivityPubIRI)},
nil,
false,
[]vocab.ActivityStreamsMention{},
[]vocab.ActivityStreamsImage{
newAPImage(
URLMustParse("http://example.org/users/some_user/statuses/afaba698-5740-4e32-a702-af61aa543bc1/attachment1.jpeg"),
"image/jpeg",
"trent reznor looking handsome as balls",
"LEDara58O=t5EMSOENEN9]}?aK%0"),
},
),
"https://unknown-instance.com/users/brand_new_person/statuses/01FE4NTHKWW7THT67EF10EB839": NewAPNote(
URLMustParse("https://unknown-instance.com/users/brand_new_person/statuses/01FE4NTHKWW7THT67EF10EB839"),
URLMustParse("https://unknown-instance.com/users/@brand_new_person/01FE4NTHKWW7THT67EF10EB839"),

View file

@ -59,7 +59,7 @@ type MockHTTPClient struct {
testRemoteServices map[string]vocab.ActivityStreamsService
testRemoteAttachments map[string]RemoteAttachmentFile
SentMessages map[string][]byte
}
// NewMockHTTPClient returns a client that conforms to the pub.HttpClient interface.
@ -71,7 +71,7 @@ type MockHTTPClient struct {
// to customize how the client is mocked.
//
// Note that you should never ever make ACTUAL http calls with this thing.
func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relativeMediaPath string) pub.HttpClient {
func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relativeMediaPath string) *MockHTTPClient {
mockHttpClient := &MockHTTPClient{}
if do != nil {
@ -85,13 +85,26 @@ func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relat
mockHttpClient.testRemoteServices = NewTestFediServices()
mockHttpClient.testRemoteAttachments = NewTestFediAttachments(relativeMediaPath)
mockHttpClient.SentMessages = make(map[string][]byte)
mockHttpClient.do = func(req *http.Request) (*http.Response, error) {
responseCode := http.StatusNotFound
responseBytes := []byte(`{"error":"404 not found"}`)
responseContentType := "application/json"
responseContentLength := len(responseBytes)
if strings.Contains(req.URL.String(), ".well-known/webfinger") {
if req.Method == http.MethodPost {
b, err := io.ReadAll(req.Body)
if err != nil {
panic(err)
}
mockHttpClient.SentMessages[req.URL.String()] = b
responseCode = http.StatusOK
responseBytes = []byte(`{"ok":"accepted"}`)
responseContentType = "application/json"
responseContentLength = len(responseBytes)
} else if strings.Contains(req.URL.String(), ".well-known/webfinger") {
responseCode, responseBytes, responseContentType, responseContentLength = WebfingerResponse(req)
} else if note, ok := mockHttpClient.testRemoteStatuses[req.URL.String()]; ok {
// the request is for a note that we have stored
@ -103,8 +116,10 @@ func NewMockHTTPClient(do func(req *http.Request) (*http.Response, error), relat
if err != nil {
panic(err)
}
responseCode = http.StatusOK
responseBytes = noteJson
responseContentType = "application/activity+json"
responseContentLength = len(noteJson)
} else if person, ok := mockHttpClient.testRemotePeople[req.URL.String()]; ok {
// the request is for a person that we have stored
personI, err := streams.Serialize(person)