Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-workhorse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-workhorse
Commits
8523a84f
Commit
8523a84f
authored
Dec 07, 2018
by
Jacob Vosmaer
Committed by
Nick Thomas
Dec 07, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove curl from sendfile_test.go
parent
ca30d639
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
37 deletions
+37
-37
sendfile_test.go
sendfile_test.go
+37
-37
No files found.
sendfile_test.go
View file @
8523a84f
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"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment