0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 23:33:15 -05:00

[GITEA] Fix API inconsistencies

- Document the correct content types for Git archives. Add code that
actually sets the correct application type for `.zip` and `.tar.gz`.
- When an action (POST/PUT/DELETE method) was successful, an 204 status
code should be returned instead of status code 200.
- Add and adjust integration testing.
- Resolves #2180
- Resolves #2181

(cherry picked from commit 6c8c4512b5)
(cherry picked from commit 3f74bcb14d)
(cherry picked from commit 6ed9057fd7)
This commit is contained in:
Gusted 2024-01-19 01:14:49 +01:00 committed by Earl Warren
parent 0b503e5e86
commit bbe5a881cc
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
3 changed files with 19 additions and 2 deletions

View file

@ -257,7 +257,9 @@ func GetArchive(ctx *context.APIContext) {
// --- // ---
// summary: Get an archive of a repository // summary: Get an archive of a repository
// produces: // produces:
// - application/json // - application/octet-stream
// - application/zip
// - application/gzip
// parameters: // parameters:
// - name: owner // - name: owner
// in: path // in: path
@ -337,7 +339,17 @@ func download(ctx *context.APIContext, archiveName string, archiver *repo_model.
} }
defer fr.Close() defer fr.Close()
contentType := ""
switch archiver.Type {
case git.ZIP:
contentType = "application/zip"
case git.TARGZ:
// Per RFC6713.
contentType = "application/gzip"
}
ctx.ServeContent(fr, &context.ServeHeaderOptions{ ctx.ServeContent(fr, &context.ServeHeaderOptions{
ContentType: contentType,
Filename: downloadName, Filename: downloadName,
LastModified: archiver.CreatedUnix.AsLocalTime(), LastModified: archiver.CreatedUnix.AsLocalTime(),
}) })

View file

@ -3533,7 +3533,9 @@
"/repos/{owner}/{repo}/archive/{archive}": { "/repos/{owner}/{repo}/archive/{archive}": {
"get": { "get": {
"produces": [ "produces": [
"application/json" "application/octet-stream",
"application/zip",
"application/gzip"
], ],
"tags": [ "tags": [
"repository" "repository"

View file

@ -32,18 +32,21 @@ func TestAPIDownloadArchive(t *testing.T) {
bs, err := io.ReadAll(resp.Body) bs, err := io.ReadAll(resp.Body)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, bs, 320) assert.Len(t, bs, 320)
assert.EqualValues(t, "application/zip", resp.Header().Get("Content-Type"))
link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master.tar.gz", user2.Name, repo.Name)) link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master.tar.gz", user2.Name, repo.Name))
resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK) resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK)
bs, err = io.ReadAll(resp.Body) bs, err = io.ReadAll(resp.Body)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, bs, 266) assert.Len(t, bs, 266)
assert.EqualValues(t, "application/gzip", resp.Header().Get("Content-Type"))
link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master.bundle", user2.Name, repo.Name)) link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master.bundle", user2.Name, repo.Name))
resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK) resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK)
bs, err = io.ReadAll(resp.Body) bs, err = io.ReadAll(resp.Body)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, bs, 382) assert.Len(t, bs, 382)
assert.EqualValues(t, "application/octet-stream", resp.Header().Get("Content-Type"))
link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master", user2.Name, repo.Name)) link, _ = url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/archive/master", user2.Name, repo.Name))
MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusBadRequest) MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusBadRequest)