Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-shell
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
nexedi
gitlab-shell
Commits
0ddb3c76
Commit
0ddb3c76
authored
Jul 19, 2018
by
Ash McKenzie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support HTTP headers and sending JSON
parent
e865251e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
24 deletions
+79
-24
lib/gitlab_net.rb
lib/gitlab_net.rb
+1
-1
lib/http_helper.rb
lib/http_helper.rb
+16
-10
spec/gitlab_net_spec.rb
spec/gitlab_net_spec.rb
+62
-13
No files found.
lib/gitlab_net.rb
View file @
0ddb3c76
...
...
@@ -91,7 +91,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
end
def
check
get
(
"
#{
internal_api_endpoint
}
/check"
,
read_timeout:
CHECK_TIMEOUT
)
get
(
"
#{
internal_api_endpoint
}
/check"
,
options:
{
read_timeout:
CHECK_TIMEOUT
}
)
end
def
authorized_key
(
key
)
...
...
lib/http_helper.rb
View file @
0ddb3c76
...
...
@@ -33,15 +33,19 @@ module HTTPHelper
http
end
def
http_request_for
(
method
,
uri
,
params
=
{})
def
http_request_for
(
method
,
uri
,
params
:
{},
headers:
{},
options:
{})
request_klass
=
method
==
:get
?
Net
::
HTTP
::
Get
:
Net
::
HTTP
::
Post
request
=
request_klass
.
new
(
uri
.
request_uri
)
request
=
request_klass
.
new
(
uri
.
request_uri
,
headers
)
user
=
config
.
http_settings
[
'user'
]
password
=
config
.
http_settings
[
'password'
]
request
.
basic_auth
(
user
,
password
)
if
user
&&
password
request
.
set_form_data
(
params
.
merge
(
secret_token:
secret_token
))
if
options
[
:json
]
request
.
body
=
options
[
:json
].
merge
(
secret_token:
secret_token
).
to_json
else
request
.
set_form_data
(
params
.
merge
(
secret_token:
secret_token
))
end
if
uri
.
is_a?
(
URI
::
HTTPUNIX
)
# The HTTPUNIX HTTP client does not set a correct Host header. This can
...
...
@@ -52,13 +56,15 @@ module HTTPHelper
request
end
def
request
(
method
,
url
,
params
=
{},
options
=
{})
def
request
(
method
,
url
,
params
:
{},
headers:
{},
options:
{})
$logger
.
debug
(
'Performing request'
,
method:
method
.
to_s
.
upcase
,
url:
url
)
uri
=
URI
.
parse
(
url
)
http
=
http_client_for
(
uri
,
options
)
request
=
http_request_for
(
method
,
uri
,
params
)
request
=
http_request_for
(
method
,
uri
,
params:
params
,
headers:
headers
,
options:
options
)
begin
start_time
=
Time
.
new
...
...
@@ -79,12 +85,12 @@ module HTTPHelper
response
end
def
get
(
url
,
options
=
{})
request
(
:get
,
url
,
{},
options
)
def
get
(
url
,
headers:
{},
options:
{})
request
(
:get
,
url
,
headers:
headers
,
options:
options
)
end
def
post
(
url
,
params
)
request
(
:post
,
url
,
params
)
def
post
(
url
,
params
,
headers:
{},
options:
{}
)
request
(
:post
,
url
,
params
:
params
,
headers:
headers
,
options:
options
)
end
def
cert_store
...
...
spec/gitlab_net_spec.rb
View file @
0ddb3c76
...
...
@@ -5,6 +5,7 @@ require_relative '../lib/gitlab_access_status'
describe
GitlabNet
,
vcr:
true
do
let
(
:gitlab_net
)
{
GitlabNet
.
new
}
let
(
:changes
)
{
[
'0000000000000000000000000000000000000000 92d0970eefd7acb6d548878925ce2208cfe2d2ec refs/heads/branch4'
]
}
let
(
:base_api_endpoint
)
{
'http://localhost:3000/api/v4'
}
let
(
:internal_api_endpoint
)
{
'http://localhost:3000/api/v4/internal'
}
let
(
:project
)
{
'gitlab-org/gitlab-test.git'
}
let
(
:key
)
{
'key-1'
}
...
...
@@ -12,7 +13,7 @@ describe GitlabNet, vcr: true do
let
(
:secret
)
{
"0a3938d9d95d807e94d937af3a4fbbea
\n
"
}
before
do
gitlab_net
.
stub
(
:
internal_api_endpoint
).
and_return
(
internal
_api_endpoint
)
gitlab_net
.
stub
(
:
base_api_endpoint
).
and_return
(
base
_api_endpoint
)
gitlab_net
.
stub
(
:secret_token
).
and_return
(
secret
)
end
...
...
@@ -381,24 +382,72 @@ describe GitlabNet, vcr: true do
describe
:http_request_for
do
context
'with stub'
do
let
(
:get
)
do
double
(
Net
::
HTTP
::
Get
).
tap
do
|
get
|
Net
::
HTTP
::
Get
.
stub
(
:new
)
{
get
}
end
end
let
(
:get
)
{
double
(
Net
::
HTTP
::
Get
)
}
let
(
:user
)
{
'user'
}
let
(
:password
)
{
'password'
}
let
(
:url
)
{
URI
'http://localhost/'
}
subject
{
gitlab_net
.
send
:http_request_for
,
:get
,
url
}
let
(
:params
)
{
{
'key1'
=>
'value1'
}
}
let
(
:headers
)
{
{
'Content-Type'
=>
'application/json'
}
}
let
(
:options
)
{
{
json:
{
'key2'
=>
'value2'
}
}
}
context
'with no params, options or headers'
do
subject
{
gitlab_net
.
send
:http_request_for
,
:get
,
url
}
before
do
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'user'
)
{
user
}
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'password'
)
{
password
}
Net
::
HTTP
::
Get
.
should_receive
(
:new
).
with
(
'/'
,
{}).
and_return
(
get
)
get
.
should_receive
(
:basic_auth
).
with
(
user
,
password
).
once
get
.
should_receive
(
:set_form_data
).
with
(
hash_including
(
secret_token:
secret
)).
once
end
before
do
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'user'
)
{
user
}
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'password'
)
{
password
}
get
.
should_receive
(
:basic_auth
).
with
(
user
,
password
).
once
get
.
should_receive
(
:set_form_data
).
with
(
hash_including
(
secret_token:
secret
)).
once
it
{
should_not
be_nil
}
end
it
{
should_not
be_nil
}
context
'with params'
do
subject
{
gitlab_net
.
send
:http_request_for
,
:get
,
url
,
params:
params
,
headers:
headers
}
before
do
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'user'
)
{
user
}
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'password'
)
{
password
}
Net
::
HTTP
::
Get
.
should_receive
(
:new
).
with
(
'/'
,
headers
).
and_return
(
get
)
get
.
should_receive
(
:basic_auth
).
with
(
user
,
password
).
once
get
.
should_receive
(
:set_form_data
).
with
({
'key1'
=>
'value1'
,
secret_token:
secret
}).
once
end
it
{
should_not
be_nil
}
end
context
'with headers'
do
subject
{
gitlab_net
.
send
:http_request_for
,
:get
,
url
,
headers:
headers
}
before
do
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'user'
)
{
user
}
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'password'
)
{
password
}
Net
::
HTTP
::
Get
.
should_receive
(
:new
).
with
(
'/'
,
headers
).
and_return
(
get
)
get
.
should_receive
(
:basic_auth
).
with
(
user
,
password
).
once
get
.
should_receive
(
:set_form_data
).
with
(
hash_including
(
secret_token:
secret
)).
once
end
it
{
should_not
be_nil
}
end
context
'with options'
do
context
'with json'
do
subject
{
gitlab_net
.
send
:http_request_for
,
:get
,
url
,
options:
options
}
before
do
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'user'
)
{
user
}
gitlab_net
.
send
(
:config
).
http_settings
.
stub
(
:[]
).
with
(
'password'
)
{
password
}
Net
::
HTTP
::
Get
.
should_receive
(
:new
).
with
(
'/'
,
{}).
and_return
(
get
)
get
.
should_receive
(
:basic_auth
).
with
(
user
,
password
).
once
get
.
should_receive
(
:body
=
).
with
({
'key2'
=>
'value2'
,
secret_token:
secret
}.
to_json
).
once
get
.
should_not_receive
(
:set_form_data
)
end
it
{
should_not
be_nil
}
end
end
end
context
'Unix socket'
do
...
...
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