75f3dd141c
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CI and Release / lint-frontend (push) Has been cancelled
CI and Release / dockerfile-scan (push) Has been cancelled
CI and Release / test-frontend (push) Has been cancelled
CI and Release / lint-verification-agent (push) Has been cancelled
CI and Release / test-verification-agent (push) Has been cancelled
CI and Release / e2e-verification-agent (push) Has been cancelled
CI and Release / test-backend (push) Has been cancelled
CI and Release / build-dev-image (push) Has been cancelled
CI and Release / push-dev-image (push) Has been cancelled
CI and Release / build-image (push) Has been cancelled
CI and Release / build-verification-image (push) Has been cancelled
CI and Release / determine-version (push) Has been cancelled
CI and Release / push-image (push) Has been cancelled
CI and Release / push-verification-image (12) (push) Has been cancelled
CI and Release / lint-backend (push) Has been cancelled
CI and Release / push-verification-image (17) (push) Has been cancelled
CI and Release / push-verification-image (18) (push) Has been cancelled
CI and Release / release (push) Has been cancelled
CI and Release / publish-helm-chart (push) Has been cancelled
CI and Release / push-verification-image (13) (push) Has been cancelled
CI and Release / push-verification-image (14) (push) Has been cancelled
CI and Release / push-verification-image (15) (push) Has been cancelled
CI and Release / push-verification-image (16) (push) Has been cancelled
91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package mailpit
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const requestTimeout = 5 * time.Second
|
|
|
|
type Message struct {
|
|
ID string `json:"ID"`
|
|
From Address `json:"From"`
|
|
To []Address `json:"To"`
|
|
Subject string `json:"Subject"`
|
|
Snippet string `json:"Snippet"`
|
|
}
|
|
|
|
type Address struct {
|
|
Name string `json:"Name"`
|
|
Address string `json:"Address"`
|
|
}
|
|
|
|
type messagesResponse struct {
|
|
Messages []Message `json:"messages"`
|
|
}
|
|
|
|
// Client queries a Mailpit instance's HTTP API to assert what mail a test delivered.
|
|
type Client struct {
|
|
baseURL string
|
|
}
|
|
|
|
// NewClient builds a Mailpit API client for httpEndpoint (host:port of the container's HTTP port).
|
|
func NewClient(httpEndpoint string) *Client {
|
|
return &Client{baseURL: "http://" + httpEndpoint}
|
|
}
|
|
|
|
func (c *Client) FetchMessages() ([]Message, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
defer cancel()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/api/v1/messages", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("mailpit fetch: %w", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return nil, fmt.Errorf("mailpit fetch returned %s: %s", resp.Status, string(body))
|
|
}
|
|
|
|
var payload messagesResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
|
|
return nil, fmt.Errorf("mailpit decode: %w", err)
|
|
}
|
|
|
|
return payload.Messages, nil
|
|
}
|
|
|
|
func (c *Client) Clear() error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
defer cancel()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, c.baseURL+"/api/v1/messages", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("mailpit clear: %w", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return fmt.Errorf("mailpit clear returned %s: %s", resp.Status, string(body))
|
|
}
|
|
|
|
return nil
|
|
}
|