Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an http test method #1403

Merged
merged 1 commit into from
Jan 26, 2017
Merged

Add an http test method #1403

merged 1 commit into from
Jan 26, 2017

Conversation

guilhem
Copy link
Contributor

@guilhem guilhem commented Jan 5, 2017

fix #336

Copy link
Contributor

@chris-rock chris-rock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guilhem This is a great addition to our suite. Lets try to use native ruby classes instead of the requests gem. I propose to reuse http helper already used for the compliance plugin. We could move the helper to inspec/lib/utils https://github.com/chef/inspec/tree/master/lib/utils

name 'http'
desc 'Use the http InSpec audit resource to test http call.'
example "
describe http(http://localhost:8080/ping', auth: ['user, 'test'], params: {format: 'html'}) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better:

describe http('http://localhost:8080/ping', auth: ['user, 'test'], params: {format: 'html'}) do
        its('status') { should cmp 200 }
        its('body') { should cmp 'pong' }
        its('Content-Type') { should include('text/html') }
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

@@ -38,4 +38,5 @@ Gem::Specification.new do |spec|
spec.add_dependency 'sslshake', '~> 1'
spec.add_dependency 'parallel', '~> 1.9'
spec.add_dependency 'rspec_junit_formatter', '~> 0.2.3'
spec.add_dependency 'requests', '~> 1.0.0'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need the requests module? It is not maintained for a couple of years anymore. See https://github.com/cyx/requests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chris-rock yes it isn't well maintained but it works like a charm.
I try to use native Net::HTTP but it finally was quite the same as requests... with less stability.
I also try to find another lib without success.

requests wasn't a wonderful choice but a pragmatic one. :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chris-rock or I just find http://www.rubydoc.info/gems/http/HTTP/Request https://github.com/httprb/http who is:

  • quite maintained :)
  • some more work to use it "as it" (basic auth etc) :|
  • has some dependencies :(

It's up to you :)

@auth = auth
@headers = headers
@data = data
@request = Requests.request(@method, @url,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try to use Net::HTTP. We could extract some helpers and place them into inspec utils from https://github.com/chef/inspec/blob/master/lib/bundles/inspec-compliance/http.rb

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as said in other comment, doing stuff in Net::HTTP is really painful.
https, headers, path, params, method everything is in different class without good layering.
URI / Request / ...
Requests already do the stuff. If 1 day requests stop working (with a ruby X.X) it would be time to vendor it inside our code.

For the moment it seems quite overkill to refactor inside our code a lib without any other dependency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am open for another gem. Unfortunately we cannot go with the current one. Since, once merged, this PR it is part of a maintained project, we need to be sure we get all PRs in a way that we can maintain them going forward. I am open for alternative solutions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guilhem
Copy link
Contributor Author

guilhem commented Jan 6, 2017

@chris-rock I changed to use http lib :)

@chris-rock
Copy link
Contributor

@guilhem I apologize for the delayed response. I'd like your change. Can you please add documentation as well in here https://github.com/chef/inspec/tree/master/docs/resources?

@chris-rock
Copy link
Contributor

@guilhem Anything I can help you with to get this merged this week?

@guilhem
Copy link
Contributor Author

guilhem commented Jan 18, 2017

@chris-rock sorry I'm in holiday this week, but you can push anything you want in my branch :)

@chris-rock
Copy link
Contributor

@guilhem Enjoy our holiday, lets bring this in next week then.

@guilhem
Copy link
Contributor Author

guilhem commented Jan 23, 2017

@chris-rock should be ok

Copy link
Contributor

@chris-rock chris-rock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work @guilhem I just have some minor comments. Once those are fixed, we are ready to get the resource in

# encoding: utf-8
# copyright: 2017, Criteo
# author: Guilhem Lettron
# license: All rights reserved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not allow All rights reserved. Every contribution needs to be Apache 2 licensed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok for me, I just copy/pasta other resources with same lines :)

@data = data
http = HTTP.headers(@headers)
http = http.basic_auth(@auth) unless @auth.empty?
@response = http.request(@method, @url, { body: @data, params: @params })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do a lazy response request, otherwise the request will be done during resource instantiation. Lets have a private method request that is called by all other functions and it should also cache the result in @response

@chris-rock
Copy link
Contributor

@guilhem Can you rebase on latest master? functional tests are fixed there. I apologise for the inconvenience.

Signed-off-by: Guilhem Lettron <g.lettron@criteo.com>
@guilhem
Copy link
Contributor Author

guilhem commented Jan 26, 2017

@chris-rock should be ok :)

@chris-rock
Copy link
Contributor

Awesome work @guilhem

@chris-rock chris-rock merged commit f6d04e1 into inspec:master Jan 26, 2017
@guilhem guilhem deleted the http branch January 26, 2017 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HTTP request resource
2 participants