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 test suite for Jsonp middleware #6958

Draft
wants to merge 4 commits into
base: series/0.23
Choose a base branch
from
Draft

Conversation

valencik
Copy link
Member

@valencik valencik commented Feb 6, 2023

This PR add some tests for the Jsonp middleware.

Unfortunately, in doing so I encountered regex exceptions on the callback validation regex.

org.http4s.server.middleware.JsonpSuite.Wrap response when callback param matches and resp in JSON 0.05s 
java.util.regex.PatternSyntaxException: Illegal character range near index 345

These new tests pass if I simplify the regex to remove most of the character ranges.

private val ValidCallback =
  """^((?!(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$)[$A-Z_a-z]*)$""".r

@codecov-commenter
Copy link

codecov-commenter commented Feb 6, 2023

Codecov Report

Merging #6958 (02c1ec6) into series/0.23 (0e253e3) will increase coverage by 0.20%.
The diff coverage is 100.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@               Coverage Diff               @@
##           series/0.23    #6958      +/-   ##
===============================================
+ Coverage        71.22%   71.42%   +0.20%     
===============================================
  Files              340      340              
  Lines            10362    10362              
  Branches           886      886              
===============================================
+ Hits              7380     7401      +21     
+ Misses            2982     2961      -21     
Impacted Files Coverage Δ
...ain/scala/org/http4s/server/middleware/Jsonp.scala 100.00% <100.00%> (+100.00%) ⬆️
...n/scala/org/http4s/laws/discipline/arbitrary.scala 86.93% <0.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@valencik
Copy link
Member Author

valencik commented Feb 6, 2023

Still failing on native

==> X org.http4s.server.middleware.JsonpSuite.Wrap response when callback param matches and resp in JSON 0.01s java.util.regex.PatternSyntaxException: Unknown inline modifier near index 4
(?!(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$)[$A-Z_a-z]*)$
    ^
    at java.lang.StackTrace$.currentStackTrace(Unknown Source)
    at java.lang.Throwable.fillInStackTrace(Unknown Source)
    at scala.scalanative.regex.Parser.parsePerlFlags(Unknown Source)
    at scala.scalanative.regex.Parser.scala$scalanative$regex$Parser$$parseInternal(Unknown Source)
    at scala.scalanative.regex.Parser$.parse(Unknown Source)
    at scala.scalanative.regex.RE2$.compileImpl(Unknown Source)
    at scala.scalanative.regex.Pattern$.compile(Unknown Source)
    at scala.scalanative.regex.Pattern$.compile(Unknown Source)
    at java.util.regex.Pattern$.compile(Unknown Source)
    at java.util.regex.Pattern$.compile(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at scala.collection.StringOps$.r$extension(Unknown Source)
    at org.http4s.server.middleware.Jsonp$.<init>(Unknown Source)
    at <none>._SM35org.http4s.server.middleware.Jsonp$G4load(Unknown Source)
    at org.http4s.server.middleware.JsonpSuite.$anonfun$new$1(Unknown Source)

@armanbilge
Copy link
Member

Ah, regex might not be working properly on Native. It happens sometmes.

@armanbilge
Copy link
Member

?!(?:

Are these look-ahead/behinds? Those are not supported on Native IIRC.

@valencik
Copy link
Member Author

valencik commented Feb 6, 2023

Regex was changed last here:
d675393#diff-9f712c6cea40b287988742a7ad4d26f2e05a325770c0b988cc7fd8d55d0f21d0L28

So it's perhaps been broken for a while.

@rossabaker
Copy link
Member

I restored parity with the original version, because the original author is a thoughtful type and it was probably right. The original changed because Unicode escape sequences are deprecated in triple quotes. Something went wrong in the translation, so I took an alternate approach of going to single quotes.

It never worked on Native and without investment never will.

I agree with the deprecation strategy in #6959, because even if it worked splendidly, Jsonp is a horrible idea in 2023. Curiosity got the best of me, and after @valencik went to all the work of testing it, we might as well leave behind something that works.

@rossabaker
Copy link
Member

Also: I'm surprised we're not validating this with a macro. But I don't see any other production, literal regexes in the code, other than String#replace calls, so I'm not going to shave that yak right now.

@danicheg
Copy link
Member

danicheg commented Feb 6, 2023

Wow, amazing work, folks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module:server series/0.23 PRs targeting 0.23.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants