SPB Git

Repository Public

files.py 2.8 KB 59 lines python

1#!/usr/bin/env python32# SP Notes Core3# File: Scripts/recorders/files.py4# Description: Records the fixtures of GET /api/files/<id> with Range (API-NEXT §4.15): the whole file, single ranges (206),5#              an unsatisfiable range (416), If-Range, several ranges, a deleted attachment. Bodies are kept as .bin.6#7# Author: Simon-Pierre Boucher8# Contact: contact@spboucher.ai9import os10import sys11import time12import urllib.parse1314sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))15from spn_recorder import Recorder, new_id  # noqa: E402161718def text(value):19    return {"type": "paragraph", "content": [{"type": "text", "text": value}]}202122def now():23    return time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime()) + ".%03dZ" % int((time.time() % 1) * 1000)242526def q(value):27    return urllib.parse.quote(value, safe="")282930import hashlib  # noqa: E4023132r = Recorder("files")33page = r.call("POST", "/api/pages", {"id": new_id(), "title": "Fixture · fichiers", "section": "notes"}).json34# 3 000 deterministic bytes (the tests rebuild them and check the ETag = sha256 of the content).35content = b"".join(hashlib.sha256(("spn-fixture-%d" % i).encode()).digest() for i in range(94))[:3000]36att_id = new_id()37r.upload("/api/attachments", {"id": att_id, "pageId": page["id"]}, "fixture-range.bin", "application/octet-stream", content, save="upload")38r.upload("/api/attachments", {"id": att_id, "pageId": page["id"]}, "fixture-range.bin", "application/octet-stream", content, save="upload-existing")39path = "/api/files/" + att_id40etag = r.call("GET", path, save="file-whole").headers.get("etag", "")41r.call("GET", path, headers={"Range": "bytes=0-9"}, save="file-range-first")42r.call("GET", path, headers={"Range": "bytes=1000-"}, save="file-range-open")43r.call("GET", path, headers={"Range": "bytes=-16"}, save="file-range-suffix")44r.call("GET", path, headers={"Range": "bytes=2990-9999"}, save="file-range-clamped")45r.call("GET", path, headers={"Range": "bytes=3000-"}, save="file-range-416")46r.call("GET", path, headers={"Range": "bytes=1000-", "If-Range": etag}, save="file-if-range-match")47r.call("GET", path, headers={"Range": "bytes=1000-", "If-Range": '"0000000000000000000000000000000000000000000000000000000000000000"'},48       save="file-if-range-mismatch")49r.call("GET", path, headers={"Range": "bytes=0-1,5-6"}, save="file-range-multiple")50r.call("GET", path, headers={"Range": "lines=1-2"}, save="file-range-malformed")51r.call("GET", "/api/files/" + new_id(), save="file-not-found")52r.call("GET", "/api/files/not-a-uuid", save="file-bad-id")53r.call("GET", "/api/attachments?pageId=" + page["id"], save="attachments-list")54r.call("DELETE", "/api/attachments?id=" + att_id)55r.call("GET", path, save="file-deleted")56r.call("GET", path, headers={"Range": "bytes=0-9"}, save="file-deleted-range")57r.purge_pages([page["id"]])58r.finish()59
Files main
Menu

Theme