Commit 8523a84f authored by Jacob Vosmaer's avatar Jacob Vosmaer Committed by Nick Thomas

Remove curl from sendfile_test.go

parent ca30d639
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"mime"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"os/exec"
"path" "path"
"testing" "testing"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
) )
func TestDeniedLfsDownload(t *testing.T) { func TestDeniedLfsDownload(t *testing.T) {
...@@ -37,9 +37,8 @@ func allowedXSendfileDownload(t *testing.T, contentFilename string, filePath str ...@@ -37,9 +37,8 @@ func allowedXSendfileDownload(t *testing.T, contentFilename string, filePath str
// Prepare test server and backend // Prepare test server and backend
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("UPSTREAM", r.Method, r.URL) log.Println("UPSTREAM", r.Method, r.URL)
if xSendfileType := r.Header.Get("X-Sendfile-Type"); xSendfileType != "X-Sendfile" { require.Equal(t, "X-Sendfile", r.Header.Get("X-Sendfile-Type"))
t.Fatalf(`X-Sendfile-Type want "X-Sendfile" got %q`, xSendfileType)
}
w.Header().Set("X-Sendfile", contentPath) w.Header().Set("X-Sendfile", contentPath)
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, contentFilename)) w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, contentFilename))
w.Header().Set("Content-Type", fmt.Sprintf(`application/octet-stream`)) w.Header().Set("Content-Type", fmt.Sprintf(`application/octet-stream`))
...@@ -49,25 +48,20 @@ func allowedXSendfileDownload(t *testing.T, contentFilename string, filePath str ...@@ -49,25 +48,20 @@ func allowedXSendfileDownload(t *testing.T, contentFilename string, filePath str
ws := startWorkhorseServer(ts.URL) ws := startWorkhorseServer(ts.URL)
defer ws.Close() defer ws.Close()
if err := os.MkdirAll(cacheDir, 0755); err != nil { require.NoError(t, os.MkdirAll(cacheDir, 0755))
t.Fatal(err)
}
contentBytes := []byte("content") contentBytes := []byte("content")
if err := ioutil.WriteFile(contentPath, contentBytes, 0644); err != nil { require.NoError(t, ioutil.WriteFile(contentPath, contentBytes, 0644))
t.Fatal(err)
} resp, err := http.Get(fmt.Sprintf("%s/%s", ws.URL, filePath))
require.NoError(t, err)
downloadCmd := exec.Command("curl", "-J", "-O", fmt.Sprintf("%s/%s", ws.URL, filePath))
downloadCmd.Dir = scratchDir requireAttachmentName(t, resp, contentFilename)
runOrFail(t, downloadCmd)
actual, err := ioutil.ReadAll(resp.Body)
actual, err := ioutil.ReadFile(path.Join(scratchDir, contentFilename)) require.NoError(t, err)
if err != nil { require.NoError(t, resp.Body.Close())
t.Fatal(err)
} require.Equal(t, actual, contentBytes, "response body")
if !bytes.Equal(actual, contentBytes) {
t.Fatal("Unexpected file contents in download")
}
} }
func deniedXSendfileDownload(t *testing.T, contentFilename string, filePath string) { func deniedXSendfileDownload(t *testing.T, contentFilename string, filePath string) {
...@@ -76,9 +70,8 @@ func deniedXSendfileDownload(t *testing.T, contentFilename string, filePath stri ...@@ -76,9 +70,8 @@ func deniedXSendfileDownload(t *testing.T, contentFilename string, filePath stri
// Prepare test server and backend // Prepare test server and backend
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("UPSTREAM", r.Method, r.URL) log.Println("UPSTREAM", r.Method, r.URL)
if xSendfileType := r.Header.Get("X-Sendfile-Type"); xSendfileType != "X-Sendfile" { require.Equal(t, "X-Sendfile", r.Header.Get("X-Sendfile-Type"))
t.Fatalf(`X-Sendfile-Type want "X-Sendfile" got %q`, xSendfileType)
}
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, contentFilename)) w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, contentFilename))
w.WriteHeader(200) w.WriteHeader(200)
fmt.Fprint(w, "Denied") fmt.Fprint(w, "Denied")
...@@ -87,15 +80,22 @@ func deniedXSendfileDownload(t *testing.T, contentFilename string, filePath stri ...@@ -87,15 +80,22 @@ func deniedXSendfileDownload(t *testing.T, contentFilename string, filePath stri
ws := startWorkhorseServer(ts.URL) ws := startWorkhorseServer(ts.URL)
defer ws.Close() defer ws.Close()
downloadCmd := exec.Command("curl", "-J", "-O", fmt.Sprintf("%s/%s", ws.URL, filePath)) resp, err := http.Get(fmt.Sprintf("%s/%s", ws.URL, filePath))
downloadCmd.Dir = scratchDir require.NoError(t, err)
runOrFail(t, downloadCmd)
requireAttachmentName(t, resp, contentFilename)
actual, err := ioutil.ReadFile(path.Join(scratchDir, contentFilename))
if err != nil { actual, err := ioutil.ReadAll(resp.Body)
t.Fatal(err) require.NoError(t, err, "read body")
} require.NoError(t, resp.Body.Close())
if !bytes.Equal(actual, []byte("Denied")) {
t.Fatal("Unexpected file contents in download") require.Equal(t, []byte("Denied"), actual, "response body")
} }
func requireAttachmentName(t *testing.T, resp *http.Response, filename string) {
mediaType, params, err := mime.ParseMediaType(resp.Header.Get("Content-Disposition"))
require.NoError(t, err)
require.Equal(t, "attachment", mediaType)
require.Equal(t, filename, params["filename"], "filename")
} }
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