Commit Graph

6148 Commits

Author SHA1 Message Date
Andrey Novikov
cb9d0e4864
Fix inconsistent results when parsing large durations and constructing durations from code
ActiveSupport::Duration.parse('P3Y') == 3.years # It should be true

Duration parsing made independent from any moment of time:
Fixed length in seconds is assigned to each duration part during parsing.

Changed duration of months and years in seconds to more accurate and logical:

 1. The value of 365.2425 days in Gregorian year is more accurate
    as it accounts for every 400th non-leap year.

 2. Month's length is bound to year's duration, which makes
    sensible comparisons like `12.months == 1.year` to be `true`
    and nonsensical ones like `30.days == 1.month` to be `false`.

Calculations on times and dates with durations shouldn't be affected as
duration's numeric value isn't used in calculations, only parts are used.

Methods on `Numeric` like `2.days` now use these predefined durations
to avoid duplicating of duration constants through the codebase and
eliminate creation of intermediate durations.
2017-01-09 23:04:48 +03:00
Jon Moss
c5da641471 Update cookies helper on all HTTP requests
Regression introduced by ae29142142324545a328948e059e8b8118fd7a33 / 8363b879fe759f0645179f4521cc64795efbee6e.

Previously, cookies were only updated on `GET` requests. Now we will
update the helper for all requests, as part of `process`. Added
regression tests for all available HTTP method helpers in
`ActionController::TestCase`.
2017-01-05 17:39:12 -05:00
Akira Matsuda
5473e390d3 self. is not needed when calling its own instance method
Actually, private methods cannot be called with `self.`, so it's not just redundant, it's a bad habit in Ruby
2017-01-05 19:58:52 +09:00
Rafael Mendonça França
209bfc0a5c
Fix style guide violations 2017-01-05 00:40:24 -05:00
Julia López
f28d073a39 fix with_routing when testing api only controllers 2016-12-29 20:46:30 +01:00
Jon Moss
0713265fd1 Use next instead of break; avoid terminating whole loop
We want to avoid terminating the whole loop here, because it will cause
parameters that should be removed to not be removed, since we are
terminating early. In this specific case, `param2` is processed before
`param1` due to the reversing of `route.parts`, and since `param2` fails
the check on this line, it would previously cause the whole loop to
fail, and `param1` would still be in `parameterized_parts`. Now, we are
simply calling `next`, which is the intended behavior.

Introduced by 8ca8a2d773b942c4ea76baabe2df502a339d05b1.

Fixes #27454.
2016-12-29 10:40:47 -05:00
Rafael Mendonça França
010e246756
Fix Rubocop violations and fix documentation visibility
Some methods were added to public API in
5b14129d8d4ad302b4e11df6bd5c7891b75f393c and they should be not part of
the public API.
2016-12-28 21:53:51 -05:00
Ben Hughes
f1525dac11 Optimize Journey::Route#score
Scoring routes based on constraints repeated many type conversions that
could be performed in the outer loop.  Determinations of score and
fitness also used Array operations that required allocations.  Against
my benchmark with a large routeset, this reduced object allocations by
over 30x and wall time by over 3x.
2016-12-28 17:19:15 -08:00
Akira Matsuda
a46b2f8911 assert_equal takes expectation first 2016-12-26 11:04:56 +09:00
Akira Matsuda
019cc5960d "Use assert_nil if expecting nil from ...:in `...'. This will fail in minitest 6." 2016-12-25 13:15:56 +09:00
Akira Matsuda
630cc857c9 "Use assert_nil if expecting nil from ...:in `...'. This will fail in MT6." 2016-12-25 09:59:16 +09:00
Akira Matsuda
e8ba0c0f21 "Use assert_nil if expecting nil. This will fail in minitest 6." 2016-12-25 02:29:52 +09:00
Akira Matsuda
f2dfd5c6fd Privatize unneededly protected methods in Action Pack tests 2016-12-23 23:49:11 +09:00
Aaron Patterson
2eb0a6630a
Document and update API for skip_parameter_encoding
This commit changes `parameter_encoding` to `skip_parameter_encoding`.
`skip_parameter_encoding` will set encoding on all parameters to
ASCII-8BIT for a given action on a particular controller.  This allows
the controller to handle data when the encoding of that data is unknown,
for example file systems or truly binary parameters.
2016-12-21 12:04:00 -08:00
Matthew Draper
1bf6acb1ec Merge pull request #27414 from matthewd/fix-xml-vs-html-assertion
Revise the "XML is not HTML" test
2016-12-21 04:40:24 +10:30
Matthew Draper
695065281e Merge pull request #27408 from matthewd/charset-fix
Only default the response charset when it is first set
2016-12-20 21:34:21 +10:30
Matthew Draper
86f7789480 Revise the "XML is not HTML" test
It was depending on a side-effect of the old html-scanner, so was no
longer proving what it intended to. Instead, assert more directly about
the resulting observable difference.
2016-12-20 19:21:41 +10:30
Matthew Draper
51a9311b2d Only default the response charset when it is first set
If it is explicitly cleared (e.g., response.sending_file = true), then
we should not try to set it again.
2016-12-20 05:01:42 +10:30
Jon Moss
78c6c4b248 Do not clear HTTP_COOKIES header after request 2016-12-11 14:37:18 -05:00
Rafael Mendonça França
4eb3ef812c
Do not raise exception when content_type is a empty string
When content type header is blank we were raising an exception because
`empty?` was being called on nil.
2016-12-09 14:39:43 -05:00
yuuji.yaginuma
d71f289fb2 stop using removed render :text
Follow up to 79a5ea9eadb4d43b62afacedc0706cbe88c54496
2016-12-03 15:28:59 +09:00
yuuji.yaginuma
75607a67b4 use Gem.win_platform? to check windows Ruby platforms
`Gem.win_platform?` check if it is Windows more accurately.
Ref: https://github.com/ruby/ruby/blob/ruby_2_2/lib/rubygems.rb#L945..L952
2016-11-30 20:57:50 +09:00
yuuji.yaginuma
32225387c1 use public Module#include instead of send :include
Follow up to #18767
2016-11-27 22:20:52 +09:00
Kasper Timm Hansen
86754a8f7b Use accept header instead of mangling request path.
Instead of appending a format to the request, it's much better
to just pass a more appropriate accept header. Rails will figure
out the format from that instead.

This allows devs to use `:as` on routes that don't have a format.

Introduce an `IdentityEncoder` to avoid `if request_encoder`,
essentially a better version of the purpose of the `WWWFormEncoder`.
One that makes conceptual sense on GET requests too.

Fixes #27144.
2016-11-23 22:02:19 +01:00
Tawan Sierek
064744bef6
Fix ActionDispatch::IntegrationTest#open_session
Reset a new session directly after its creation in
`ActionDispatch::IntegrationTest#open_session`. Reset the session to a clean
state before making it available to the client's test code.

Issue #22742 reports unexpected behavior of integration tests that run multiple
sessions. For example an `ActionDispatch::Flash` instance is shared across
multiple sessions, though a client code will rightfully assume that each new
session has its own flash hash.

The following test failed due to this behavior:

    class Issue22742Test < ActionDispatch::IntegrationTest
      test 'issue #22742' do
        integration_session # initialize first session
        a = open_session
        b = open_session

        refute_same(a.integration_session, b.integration_session)
      end
    end

Instead of creating a new `ActionDispatch::Integration::Session` instance,
the same instance is shared across all newly opened test sessions. This is
due to the way how new test sessions are created in
`ActionDispatch::IntegrationTest#open_session`. The already existing
`ActionDispatch::IntegrationTest` instance is duplicated  with `Object#dup`,
This approach was introduced in commit 15c31c7639b. `Object#dup` copies the
instance variables, but not the objects they reference. Therefore this issue
only occurred when the current test instance had been tapped in such a way that
the instance variable `@integration_session` was initialized before creating the
new test session.

Close #22742

[Tawan Sierek + Sina Sadeghian]
2016-11-18 15:07:16 -05:00
Artem Rashev
b388ca41d8 Fixed CONTENT_LENGTH header in ActionController::TestRequest
CONENT_LENGTH setted by string length, which is equal to number of
characters in string but StringIO.length is byte sequence and
when payload contains non-ASCII characters, stream's length will be
different. That's why real byte length should be used for CONTENT_LENGTH
header.

Add unit test for CONTENT_LENGTH header fix

It just passes non-ascii symbols as parameters and verifies that
"CONTENT_LENGTH" header has content bytes count as value.
2016-11-15 12:50:38 +03:00
Rafael Mendonça França
ee93787580
Fix style violations 2016-11-14 13:18:31 -05:00
Rafael Mendonça França
5f32221559
Merge pull request #27007 from maclover7/jm-fix-26912
Don't error on an empty CONTENT_TYPE
2016-11-13 23:09:08 -05:00
Xavier Noria
26dd9b26ab significant speedup of AC::Parameters#permit
The current implementation of AC::Parameters#permit builds permitted hashes and
then calls permit! on them.

This filtering is recursive, so we call permit! on terminal branches, but then
ascendants call permit! on themselves when the recursion goes up the stack,
which recurses all the way down again because permit! is recursive itself.
Repeat this for every parent node and you get some scary O-something going on
that I don't even want to compute.

Instead, since the whole point of the permit recursion is to build permitted
hashes along the way and at that point you know you've just come up with a
valid filtered version, you can already switch the toggle on the spot.

I have seen 2x speedups in casual benchmarks with small structures. As the
previous description shows, the difference in performance is going to be a
function of the nesting.

Note that that the involved methods are private and used only by permit.
2016-11-12 12:39:30 +01:00
Xavier Noria
e86524c0c5 adds support for arbitrary hashes in strong parameters 2016-11-11 23:31:50 +01:00
Jon Moss
773f71c8a0 Don't error on an empty CONTENT_TYPE
This commit prevents a possible issue wherein an empty CONTENT_TYPE
header is sent in a request to a Rails application, and then `request.content_mime_type`
would return `nil`. This is because the `has_content_type?` guard method
was not properly checking the validity of a request's content type; it
was only checking to see whether or not the header existed, not whether
it had a value stored inside.

Relatedly, after an internal discussion, it was determined that the
`has_content_type?` method is not meant to be part of the public API,
and is therefore changed to a `:nodoc:` method in this commit.

The test for this behavior is a little bit ugly, for two reasons. One is
that it was difficult to determine where to place the test... I figured
the best place would be with the rest of the ParamsWrapper stuff, since
that's where the original issue was. Also, we have to do some fancy
footwork in calling `dispatch` on the test's controller manually... this
is because `ActionController::TestCase` will throw an error if you try
and pass in a nil content type, which is exactly what we are trying to
test here... Because of that, we have to manually call in to the
controller, and bypass the `post` request helper.

Fixes #26912.

This is a regression in behavior between Rails versions 4.2.x and 5.0.x,
which was introduced via [this commit](a9f28600e9).
2016-11-10 14:33:16 -05:00
Rafael França
cd9d6bed1b Merge pull request #26820 from y-yagi/add_bang_merge_to_parameters
add `ActionController::Parameters#merge!`
2016-11-10 11:07:23 -05:00
Akira Matsuda
5e758de1a5 Show what was the error when assertion has failed
this test case fails with ruby 2.4, so we want to see what's actually happening
2016-11-05 15:54:22 +09:00
Rafael Mendonça França
fe1f4b2ad5
Add more rubocop rules about whitespaces 2016-10-29 01:17:49 -02:00
Genadi Samokovarov
f99c37583d Support plain loggers in DebugExceptions
I have been seeing people setting `Logger` instances for `config.logger`
and it blowing up on `rails/web-console` usage.

Now, I doubt many folks are manually setting `ActionView::Base.logger`,
but given that `DebugExceptions` is running in a pretty fragile
environment already, having it crash (and being silent) in those cases
can be pretty tricky to trace down.

I'm proposing we verify whether the `ActionView::Base.logger` supports
silencing before we try to do it, to save us the headache of tracing it
down.
2016-10-28 23:53:06 +03:00
Rafael Mendonça França
d1700d33a0
Remove mona lisa image from the tests
This image has copyright that we are not giving so it is better to use
one image that we own the copyright.
2016-10-25 12:31:35 -02:00
Charles Oliver Nutter
fe09e7bd71 Soften this test since YAML.dump may produce keys in other orders. 2016-10-21 16:27:06 -05:00
Rafael Mendonça França
e16a5fb508
Revert "Undefine assings in IntegrationTest"
This reverts commit 5dde413e1d14c42eb87071db20d075a7b962cb01.

Reason: The gem defines it so we don't need to remove
2016-10-21 16:14:39 -03:00
yuuji.yaginuma
6d7f35ecb1 add ActionController::Parameters#merge!
This method has the same behavior as `Hash#merge!`, returns current
`ActionController::Parameters`.
2016-10-18 18:04:40 +09:00
Sammy Larbi
6fccd7b629 Allow any key in Renderer environment hash
When rendering arbitrary templates, it is helpful to not overwrite `env` keys with nil if they don't match any found in the `RACK_KEY_TRANSLATION`

This allows the developer to set the environment to exactly what is needed for rendering.
2016-10-15 07:03:33 -05:00
Pavel Evstigneev
f80ddf30dd ActionController::Parameters#deep_dup (#26567)
* ActionController::Parameters#deep_dup

* Tests for ActionController::Parameters#deep_dup

* Fix test for ActionController::Parameters#deep_dup

* More tests for ActionController::Parameters#deep_dup

[Rafael Mendonça França + Pavel Evstigneev]
2016-10-11 10:29:20 -03:00
Rafael Mendonça França
de9542acd5
Remove deprecated support to non-keyword arguments in ActionDispatch::IntegrationTest,
`#process`, `#get`, `#post`, `#patch`, `#put`, `#delete`, and `#head`.
2016-10-10 01:36:01 -03:00
Rafael Mendonça França
092033d59f
Remove deprecated ActionDispatch::IntegrationTest#*_via_redirect. 2016-10-10 01:36:00 -03:00
Rafael Mendonça França
eb52e5d42f
Remove deprecated ActionDispatch::IntegrationTest#xml_http_request 2016-10-10 01:36:00 -03:00
Rafael Mendonça França
9185b68cc6
Remove deprecated support for passing :path and route path as stings in ActionDispatch::Routing::Mapper#match 2016-10-10 01:36:00 -03:00
Rafael Mendonça França
5860b3544c
Remove deprecated support passing path as nil in ActionDispatch::Routing::Mapper#match 2016-10-10 01:36:00 -03:00
Rafael Mendonça França
a3ff9db69c
Remove deprecated cache_control argument from ActionDispatch::Static#initialize 2016-10-10 01:35:59 -03:00
Rafael Mendonça França
1b975e6a69
Remove deprecated support to passing strings to the middleware stack 2016-10-10 01:35:59 -03:00
Rafael Mendonça França
fda5afeb79
Remove deprecated code in ssl middleware 2016-10-10 01:35:59 -03:00
Rafael Mendonça França
b3d41eae4b
Deprecated ActionDispatch::ParamsParser::ParamsParser
ActionDispatch::ParamsParser class was removed in favor of
ActionDispatch::Http::Parameters so it is better to move the error
constant to the new class.
2016-10-10 01:35:58 -03:00