Adds attachment fetching/uploading
This commit is contained in:
@@ -602,3 +602,47 @@ func TestSendMessage(t *testing.T) {
|
||||
t.Fatalf("Message not found in conversation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachments(t *testing.T) {
|
||||
s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: false})
|
||||
httpServer := httptest.NewServer(s)
|
||||
|
||||
// Send upload request
|
||||
testData := "hello world!"
|
||||
attachmentDataReader := strings.NewReader(testData)
|
||||
resp, err := http.Post(httpServer.URL+"/uploadAttachment?filename=test.txt", "application/data", attachmentDataReader)
|
||||
if err != nil {
|
||||
t.Fatalf("Error uploading attachment: %s", err)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Error decoding body: %s", body)
|
||||
}
|
||||
|
||||
var response web.UploadAttachmentResponse
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
t.Fatalf("Error decoding response: %s", err)
|
||||
}
|
||||
|
||||
guid := response.TransferGUID
|
||||
|
||||
// Cleanup after ourselves
|
||||
defer s.Server.DeleteAttachment(guid)
|
||||
|
||||
// Fetch it back
|
||||
resp, err = http.Get(httpServer.URL + fmt.Sprintf("/fetchAttachment?guid=%s", guid))
|
||||
if err != nil {
|
||||
t.Fatalf("Error fetching attachment: %s", err)
|
||||
}
|
||||
|
||||
responseData, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading attachment data: %s", err)
|
||||
}
|
||||
|
||||
if string(responseData) != testData {
|
||||
t.Fatalf("Didn't get expected response data: %s (got %s)", testData, responseData)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user