Commit 085e5087 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Minor fixed and improvements for build artifacts implementation

parent 5ae1356e
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"archive/zip" "archive/zip"
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt"
"io" "io"
"mime" "mime"
"net/http" "net/http"
...@@ -89,7 +90,7 @@ func DownloadArtifact(myAPI *api.API) http.Handler { ...@@ -89,7 +90,7 @@ func DownloadArtifact(myAPI *api.API) http.Handler {
http.NotFound(w, r) http.NotFound(w, r)
return return
} else if err != nil { } else if err != nil {
helper.Fail500(w, err) helper.Fail500(w, fmt.Errorf("DownloadArtifact: %v", err))
} }
}, "") }, "")
} }
...@@ -29,7 +29,7 @@ func testArtifactDownloadServer(t *testing.T, archive string, entry string) *htt ...@@ -29,7 +29,7 @@ func testArtifactDownloadServer(t *testing.T, archive string, entry string) *htt
Entry: base64.StdEncoding.EncodeToString([]byte(entry)), Entry: base64.StdEncoding.EncodeToString([]byte(entry)),
}) })
if err != nil { if err != nil {
t.Fatal("Expected to marshal") t.Fatal(err)
} }
w.Write(data) w.Write(data)
}) })
...@@ -69,7 +69,14 @@ func TestDownloadingFromValidArchive(t *testing.T) { ...@@ -69,7 +69,14 @@ func TestDownloadingFromValidArchive(t *testing.T) {
response := testDownloadArtifact(t, ts) response := testDownloadArtifact(t, ts)
testhelper.AssertResponseCode(t, response, 200) testhelper.AssertResponseCode(t, response, 200)
testhelper.AssertResponseHeader(t, response, "Content-Type", "text/plain; charset=utf-8")
testhelper.AssertResponseHeader(t, response,
"Content-Type",
"text/plain; charset=utf-8")
testhelper.AssertResponseHeader(t, response,
"Content-Disposition",
"attachment; filename=\"test.txt\"")
testhelper.AssertResponseBody(t, response, "testtest") testhelper.AssertResponseBody(t, response, "testtest")
} }
......
...@@ -12,18 +12,19 @@ import ( ...@@ -12,18 +12,19 @@ import (
"os" "os"
) )
// The artifactsFormFilter allows to pass only the `file` as file in body
type artifactsUploadProcessor struct { type artifactsUploadProcessor struct {
TempPath string TempPath string
metadataFile string metadataFile string
} }
func (a *artifactsUploadProcessor) ProcessFile(formName, fileName string, writer *multipart.Writer) error { func (a *artifactsUploadProcessor) ProcessFile(formName, fileName string, writer *multipart.Writer) error {
// ProcessFile for artifacts requires file form-data field name to eq `file`
if formName != "file" { if formName != "file" {
return fmt.Errorf("Invalid form field: %q", formName) return fmt.Errorf("Invalid form field: %q", formName)
} }
if a.metadataFile != "" { if a.metadataFile != "" {
return fmt.Errorf("Multiple files") return fmt.Errorf("Artifacts request contains more than one file!")
} }
// Create temporary file for metadata and store it's path // Create temporary file for metadata and store it's path
......
...@@ -71,7 +71,7 @@ func testArtifactsUploadServer(t *testing.T, tempPath string) *httptest.Server { ...@@ -71,7 +71,7 @@ func testArtifactsUploadServer(t *testing.T, tempPath string) *httptest.Server {
w.WriteHeader(404) w.WriteHeader(404)
return return
} }
if !bytes.Contains(metadata, []byte(metadataHeader)) { if !bytes.HasPrefix(metadata[4:], []byte(metadataHeader)) {
w.WriteHeader(400) w.WriteHeader(400)
return return
} }
......
...@@ -75,12 +75,9 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te ...@@ -75,12 +75,9 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te
file.Close() file.Close()
if filter != nil { if err := filter.ProcessFile(name, file.Name(), writer); err != nil {
err = filter.ProcessFile(name, file.Name(), writer)
if err != nil {
return cleanup, err return cleanup, err
} }
}
} else { } else {
np, err := writer.CreatePart(p.Header) np, err := writer.CreatePart(p.Header)
if err != nil { if err != nil {
...@@ -92,14 +89,11 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te ...@@ -92,14 +89,11 @@ func rewriteFormFilesFromMultipart(r *http.Request, writer *multipart.Writer, te
return cleanup, err return cleanup, err
} }
if filter != nil { if err := filter.ProcessField(name, writer); err != nil {
err = filter.ProcessField(name, writer)
if err != nil {
return cleanup, err return cleanup, err
} }
} }
} }
}
return cleanup, nil return cleanup, nil
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment