MCPcopy Create free account
hub / github.com/Mnexa-AI/e2a / TestAttachment_DownloadRoute

Function TestAttachment_DownloadRoute

internal/httpapi/attachment_endpoint_test.go:177–214  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

175}
176
177func TestAttachment_DownloadRoute(t *testing.T) {
178 srv := attTestServer(t)
179 _, meta := getJSONAtt(t, srv.URL+"/v1/agents/support%40acme.com/messages/msg_att/attachments/0", "acct")
180 dlPath := downloadPathFromURL(t, meta["download_url"].(string))
181
182 // Happy path: capability token streams the bytes (no bearer).
183 resp, err := http.Get(srv.URL + dlPath)
184 if err != nil {
185 t.Fatal(err)
186 }
187 b, _ := io.ReadAll(resp.Body)
188 resp.Body.Close()
189 if resp.StatusCode != 200 || string(b) != "%PDF-1.4 hello" {
190 t.Fatalf("download want 200 + pdf bytes, got %d %q", resp.StatusCode, b)
191 }
192 if ct := resp.Header.Get("Content-Type"); ct != "application/pdf" {
193 t.Errorf("content-type: got %q", ct)
194 }
195 if cd := resp.Header.Get("Content-Disposition"); !strings.Contains(cd, "report.pdf") {
196 t.Errorf("content-disposition: got %q", cd)
197 }
198
199 // Bad token → 403.
200 bad := strings.Replace(dlPath, "token=", "token=bogus", 1)
201 r2, _ := http.Get(srv.URL + bad)
202 r2.Body.Close()
203 if r2.StatusCode != http.StatusForbidden {
204 t.Errorf("bad token want 403, got %d", r2.StatusCode)
205 }
206
207 // A valid token for index 0 must NOT work on index 1's path (token binds index).
208 wrongIdx := strings.Replace(dlPath, "/attachments/0/download", "/attachments/1/download", 1)
209 r3, _ := http.Get(srv.URL + wrongIdx)
210 r3.Body.Close()
211 if r3.StatusCode != http.StatusForbidden {
212 t.Errorf("index-mismatched token want 403, got %d", r3.StatusCode)
213 }
214}
215
216// TestAttachment_DownloadRateLimited: the raw download route (outside the Huma
217// rate-limit middleware) is throttled per-IP. The 3rd request over a cap of 2 is

Callers

nothing calls this directly

Calls 5

attTestServerFunction · 0.85
getJSONAttFunction · 0.85
downloadPathFromURLFunction · 0.85
GetMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected