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