-
Notifications
You must be signed in to change notification settings - Fork 122
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
342 - (OC Client) Perform GET instead of POST for single component #347
342 - (OC Client) Perform GET instead of POST for single component #347
Conversation
Hey @matteofigus, I have really no idea why it is failing. I don't think my changes should affect the failing test in any way :-( Also one of the tests is showing green but instead of green tick it has three green '���' in front of it?! |
Just hitting re-run worked ;) |
Awesome, thanks :-) |
67bbebe
to
831a8c2
Compare
Hi @matteofigus, can we merge this one as it becomes a kind of a blocker for us? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for waiting and sorry for taking so long to review.
I added a couple of comments and requested some changes, but overall it seems pretty good.
test/acceptance/client.js
Outdated
@@ -612,27 +654,15 @@ describe('The node.js OC client', function(){ | |||
it('should contain the error details', function(){ | |||
|
|||
var expectedRequestWithExtraParams = { | |||
url: 'http://localhost:1234', | |||
method: 'post', | |||
url: 'http://localhost:1234/hello-world/~1.0.0??hi=john', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, not sure I understand why there are 2 ?. What's wrong with 1?
Or, what about http://localhost:1234/hello-world/~1.0.0/?hi=john
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's because expectedResults are actually regular expressions and '?' is a special symbol so we need to escape it. If we have an expected result like 'some ? text' we need to turn it into 'some \\? text' before the final processing (single back slash doesn't work because '\?' equals '?'). However we also run the getRegExpFromJson(x) function on it, which employs JSON.stringify() that in turn will additionally escape every '\' with '\\' and our string becomes 'some \\\\? text' internally.
That's why I decided to escape it with double question mark '??'.
If you are asking if we can escape '?' with '/?' that's possible, too.
Any other ideas are welcome? :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it this is only for escaping that it is fine. Perhaps, just to be 100% sure about what happens, we may create a test for a component with parameters and test the response has the correct href with the structure component/version/?params
- that could be done without regex and would be useful for tests readability I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is only for escaping the symbol in the regex and only in the tests. Regarding the second part - I'm not quite sure I understand what you are asking for...
If it's to add an additional test about if we set the right URL/href address then the only way this can be done in the client.js acceptance tests is to test for an error condition. There we are getting the request URL in the error message (exactly what's happening in the tests). However if we want to intercept and check the request then we should create a unit test for get-component-data because renderComponent/s methods don't us the full response, just the data in it.
client/src/href-builder.js
Outdated
|
||
return url.resolve(registrySegment, component.name) + versionSegment + qs; | ||
return url.resolve(registrySegment, component.name || '') + versionSegment + qs; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we actually validate component.name is not empty here and in case return a validation error? No point of making a req if component.name is missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
client/src/get-components-data.js
Outdated
var urlPath = component.name + (component.version ? '/' + component.version : ''); | ||
var queryString; | ||
|
||
if (options.parameters) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have an href-builder
for building the client-side failover urls. Can we move this there perhaps in a new "server" method?
https://github.com/opentable/oc/blob/master/client/src/href-builder.js#L9
I think you may be 1) able to reuse most of that stuff and perhaps refactor a bit as I think the only thing changing is the base url 2) keep the same convention which is version/?params instead of version??params
@@ -38,7 +38,8 @@ describe('The node.js OC client', function(){ | |||
var getRegExpFromJson = function(x){ | |||
return JSON.stringify(x) | |||
.replace(/\+/g, '\\+') | |||
.replace(/\[/g, '\\['); | |||
.replace(/\[/g, '\\[') | |||
.replace(/\?\?/g, '\\?'); //In our json regexp in order to preserve a single ? we are escaping it with ?? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big fan I guess, see next comment where I ask for clarification
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matteofigus, I did some changes and introduced new tests to cover the expectations we have for get-component-data to prepare a GET request when just one component is passed and for href-builder to include the component parameters as URL query params for the GET request URL.
However I can't come up with an idea for getting rid of this '??' escaping. Perhaps I don't understand your question entirely :-(
c43bc85
to
bdfae81
Compare
client/src/href-builder.js
Outdated
var queryString; | ||
|
||
if (options.parameters) { | ||
queryString = Object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer using querystring.stringify
as in line 36: qs = !!component.parameters ? ('/?' + querystring.stringify(component.parameters)) : '';
if you don't mind?
test/acceptance/client.js
Outdated
@@ -612,27 +654,15 @@ describe('The node.js OC client', function(){ | |||
it('should contain the error details', function(){ | |||
|
|||
var expectedRequestWithExtraParams = { | |||
url: 'http://localhost:1234', | |||
method: 'post', | |||
url: 'http://localhost:1234/hello-world/~1.0.0??hi=john', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it this is only for escaping that it is fine. Perhaps, just to be 100% sure about what happens, we may create a test for a component with parameters and test the response has the correct href with the structure component/version/?params
- that could be done without regex and would be useful for tests readability I think?
ddbdecf
to
6e9d087
Compare
…ust one component (opencomponents#342) Also: - Extended get-components-data error handling to anticipate a flat response with a single error field when a major 4xx error is detected for the request. In this case we extend the error message to provide as much data as possible. - Added more tests and fixed the existing ones.
- added a validation for null or empty component.name - added a validation for clientRenderingOptionsNotSet - the method is changed to not return null results - instead it will throw an exception
- Added a test to validate that getting the data for just one component will result in a href-builder.prepareGetRequest call and that the result of it will subsequently be used as a URL for the actual call. - Added a test to validate that getting more than one component (two components) will not result in calling href-builder.prepareGetRequest and will construct the resulting URL based only on the URL returned by href-builder.server method.
- prepareServerGet was changed to use querystring.stringify for addign the component parameters as URL query params. - change the returned URL to include a trailing '/' before the URL query params (and thus making it to follow the same pattern as in client method) - changed the client.js acceptance test according to the aforementioned changes - added client-href-builder.js unit test
ada38c0
to
d1e4949
Compare
Thanks @ivan-dimitrov-skyscanner this is a great work! |
Also:
with a single error field when a major 4xx error is detected for the request.
In this case we extend the error message to provide as much data as possible.