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
0
Merge Requests
0
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
Boxiang Sun
gitlab-ce
Commits
450030e9
Commit
450030e9
authored
Jul 27, 2018
by
Stan Hu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Accept header for JSON
parent
d27ae9f5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
7 deletions
+16
-7
lib/bitbucket_server/connection.rb
lib/bitbucket_server/connection.rb
+6
-1
spec/lib/bitbucket_server/connection_spec.rb
spec/lib/bitbucket_server/connection_spec.rb
+10
-6
No files found.
lib/bitbucket_server/connection.rb
View file @
450030e9
...
...
@@ -20,6 +20,7 @@ module BitbucketServer
def
get
(
path
,
extra_query
=
{})
response
=
Gitlab
::
HTTP
.
get
(
build_url
(
path
),
basic_auth:
auth
,
headers:
accept_headers
,
query:
extra_query
)
check_errors!
(
response
)
...
...
@@ -74,8 +75,12 @@ module BitbucketServer
@auth
||=
{
username:
username
,
password:
token
}
end
def
accept_headers
@accept_headers
||=
{
'Accept'
=>
'application/json'
}
end
def
post_headers
@post_headers
||=
{
'Content-Type'
=>
'application/json'
}
@post_headers
||=
accept_headers
.
merge
({
'Content-Type'
=>
'application/json'
})
end
def
build_url
(
path
)
...
...
spec/lib/bitbucket_server/connection_spec.rb
View file @
450030e9
...
...
@@ -10,46 +10,50 @@ describe BitbucketServer::Connection do
describe
'#get'
do
it
'returns JSON body'
do
WebMock
.
stub_request
(
:get
,
url
).
to_return
(
body:
payload
.
to_json
,
status:
200
,
headers:
headers
)
WebMock
.
stub_request
(
:get
,
url
).
with
(
headers:
{
'Accept'
=>
'application/json'
}).
to_return
(
body:
payload
.
to_json
,
status:
200
,
headers:
headers
)
expect
(
subject
.
get
(
url
,
{
something:
1
})).
to
eq
(
payload
)
end
it
'throws an exception if the response is not 200'
do
WebMock
.
stub_request
(
:get
,
url
).
to_return
(
body:
payload
.
to_json
,
status:
500
,
headers:
headers
)
WebMock
.
stub_request
(
:get
,
url
).
with
(
headers:
{
'Accept'
=>
'application/json'
}).
to_return
(
body:
payload
.
to_json
,
status:
500
,
headers:
headers
)
expect
{
subject
.
get
(
url
)
}.
to
raise_error
(
described_class
::
ConnectionError
)
end
end
describe
'#post'
do
let
(
:headers
)
{
{
'Accept'
=>
'application/json'
,
'Content-Type'
=>
'application/json'
}
}
it
'returns JSON body'
do
WebMock
.
stub_request
(
:post
,
url
).
to_return
(
body:
payload
.
to_json
,
status:
200
,
headers:
headers
)
WebMock
.
stub_request
(
:post
,
url
).
with
(
headers:
headers
).
to_return
(
body:
payload
.
to_json
,
status:
200
,
headers:
headers
)
expect
(
subject
.
post
(
url
,
payload
)).
to
eq
(
payload
)
end
it
'throws an exception if the response is not 200'
do
WebMock
.
stub_request
(
:post
,
url
).
to_return
(
body:
payload
.
to_json
,
status:
500
,
headers:
headers
)
WebMock
.
stub_request
(
:post
,
url
).
with
(
headers:
headers
).
to_return
(
body:
payload
.
to_json
,
status:
500
,
headers:
headers
)
expect
{
subject
.
post
(
url
,
payload
)
}.
to
raise_error
(
described_class
::
ConnectionError
)
end
end
describe
'#delete'
do
let
(
:headers
)
{
{
'Accept'
=>
'application/json'
,
'Content-Type'
=>
'application/json'
}
}
context
'branch API'
do
let
(
:branch_path
)
{
'/projects/foo/repos/bar/branches'
}
let
(
:branch_url
)
{
'https://test:7990/rest/branch-utils/1.0/projects/foo/repos/bar/branches'
}
let
(
:path
)
{
}
it
'returns JSON body'
do
WebMock
.
stub_request
(
:delete
,
branch_url
).
to_return
(
body:
payload
.
to_json
,
status:
200
,
headers:
headers
)
WebMock
.
stub_request
(
:delete
,
branch_url
).
with
(
headers:
headers
).
to_return
(
body:
payload
.
to_json
,
status:
200
,
headers:
headers
)
expect
(
subject
.
delete
(
:branches
,
branch_path
,
payload
)).
to
eq
(
payload
)
end
it
'throws an exception if the response is not 200'
do
WebMock
.
stub_request
(
:delete
,
branch_url
).
to_return
(
body:
payload
.
to_json
,
status:
500
,
headers:
headers
)
WebMock
.
stub_request
(
:delete
,
branch_url
).
with
(
headers:
headers
).
to_return
(
body:
payload
.
to_json
,
status:
500
,
headers:
headers
)
expect
{
subject
.
delete
(
:branches
,
branch_path
,
payload
)
}.
to
raise_error
(
described_class
::
ConnectionError
)
end
...
...
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