Files
mailgoserver/internal/jmap/blob_test.go
T
2026-08-22 06:45:05 +01:00

99 lines
2.8 KiB
Go

package jmap_test
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strconv"
"testing"
)
func TestJMAPBlobUploadThenDownload(t *testing.T) {
srv, _, _, email, password, mailboxID := newTestJMAPServer(t)
body := []byte("hello, this is an uploaded blob")
uploadReq, err := http.NewRequest(http.MethodPost, srv.URL+"/jmap/upload/"+strconv.FormatInt(mailboxID, 10), bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
uploadReq.SetBasicAuth(email, password)
uploadResp, err := http.DefaultClient.Do(uploadReq)
if err != nil {
t.Fatal(err)
}
defer uploadResp.Body.Close()
if uploadResp.StatusCode != http.StatusOK {
t.Fatalf("expected 200, got %d", uploadResp.StatusCode)
}
var uploaded struct {
BlobID string `json:"blobId"`
Size int `json:"size"`
}
if err := json.NewDecoder(uploadResp.Body).Decode(&uploaded); err != nil {
t.Fatal(err)
}
if uploaded.Size != len(body) {
t.Errorf("expected size %d, got %d", len(body), uploaded.Size)
}
downloadReq, err := http.NewRequest(http.MethodGet, srv.URL+"/jmap/download/"+strconv.FormatInt(mailboxID, 10)+"/"+uploaded.BlobID+"/blob.txt", nil)
if err != nil {
t.Fatal(err)
}
downloadReq.SetBasicAuth(email, password)
downloadResp, err := http.DefaultClient.Do(downloadReq)
if err != nil {
t.Fatal(err)
}
defer downloadResp.Body.Close()
if downloadResp.StatusCode != http.StatusOK {
t.Fatalf("expected 200, got %d", downloadResp.StatusCode)
}
got, err := io.ReadAll(downloadResp.Body)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, body) {
t.Errorf("expected byte-for-byte round trip, got %q want %q", got, body)
}
}
func TestJMAPBlobDownloadAttachment(t *testing.T) {
srv, _, store, email, password, mailboxID := newTestJMAPServer(t)
raw := []byte("From: Alice <alice@example.com>\r\n" +
"Subject: With attachment\r\n" +
"Content-Type: multipart/mixed; boundary=\"B\"\r\n\r\n" +
"--B\r\nContent-Type: text/plain\r\n\r\nsee attached\r\n" +
"--B\r\nContent-Type: text/plain\r\nContent-Disposition: attachment; filename=\"notes.txt\"\r\n\r\n" +
"attachment contents\r\n" +
"--B--\r\n")
uid, err := store.StoreMessage(mailboxID, "INBOX", raw, "<att@example.com>", "Alice <alice@example.com>", "With attachment")
if err != nil {
t.Fatal(err)
}
blobID := strconv.FormatInt(uid, 10) + "-0"
req, err := http.NewRequest(http.MethodGet, srv.URL+"/jmap/download/"+strconv.FormatInt(mailboxID, 10)+"/"+blobID+"/notes.txt", nil)
if err != nil {
t.Fatal(err)
}
req.SetBasicAuth(email, password)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
got, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(got) != "attachment contents" {
t.Errorf("expected the attachment's real bytes, got %q", got)
}
}