Bug 221720 - [LayoutTests] Convert http/tests/storageAccess convert PHP to Python
Summary: [LayoutTests] Convert http/tests/storageAccess convert PHP to Python
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Tools / Tests (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-02-10 14:36 PST by Chris Gambrell
Modified: 2021-02-12 14:13 PST (History)
6 users (show)

See Also:


Attachments
Patch (42.06 KB, patch)
2021-02-10 17:40 PST, Chris Gambrell
no flags Details | Formatted Diff | Diff
Patch (41.09 KB, patch)
2021-02-12 09:26 PST, Chris Gambrell
no flags Details | Formatted Diff | Diff
Patch (41.42 KB, patch)
2021-02-12 12:06 PST, Chris Gambrell
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Gambrell 2021-02-10 14:36:21 PST
Replacing PHP with equivalent Python CGI scripts
Comment 1 Radar WebKit Bug Importer 2021-02-10 14:51:51 PST
<rdar://problem/74207513>
Comment 2 Chris Gambrell 2021-02-10 17:40:38 PST
Created attachment 419932 [details]
Patch
Comment 3 Jonathan Bedard 2021-02-11 09:28:23 PST
Comment on attachment 419932 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=419932&action=review

> LayoutTests/http/tests/storageAccess/resources/echo-incoming-cookies-as-json.py:8
> +if 'HTTP_COOKIE' in os.environ:

I would do this instead:

header_cookies = os.environ.get('HTTP_COOKIE')
header_cookies = header_cookies.split('; ') if header_cookies else []

then you can take all of this out of the if statement

> LayoutTests/http/tests/storageAccess/resources/get-cookies.py:31
> +if not cname1:

Readability suggestion here: If you made the default for name1 the empty string (like it is now), but the defaults of name2 and name3 None, you could unify this code into a single loop:

for name in [
    query.get('name1', [''])[0],
    query.get('name2', [None])[0],
    query.get('name3', [None])[0],
]:
    if name is not None:
        continue
    cookie = cookies.get(name)
    if cookie:
        print(...)
    else:
        print(...)

> LayoutTests/http/tests/storageAccess/resources/get-cookies.py:48
> +print('<p id="output"></p>')

I think this is a good use-case for a multi-line string (ie, ''' <content> '''

> LayoutTests/http/tests/storageAccess/resources/set-cookie.py:20
> +print('<script>')

Another code case for a multi-line string.
Comment 4 Chris Gambrell 2021-02-12 09:26:13 PST
Created attachment 420134 [details]
Patch
Comment 5 Chris Gambrell 2021-02-12 09:31:08 PST
(In reply to Jonathan Bedard from comment #3)
> Comment on attachment 419932 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=419932&action=review
> 
> > LayoutTests/http/tests/storageAccess/resources/echo-incoming-cookies-as-json.py:8
> > +if 'HTTP_COOKIE' in os.environ:
> 
> I would do this instead:
> 
> header_cookies = os.environ.get('HTTP_COOKIE')
> header_cookies = header_cookies.split('; ') if header_cookies else []
> 
> then you can take all of this out of the if statement
> 

Like this change, very concise compared to my block


> > LayoutTests/http/tests/storageAccess/resources/get-cookies.py:31
> > +if not cname1:
> 
> Readability suggestion here: If you made the default for name1 the empty
> string (like it is now), but the defaults of name2 and name3 None, you could
> unify this code into a single loop:
> 
> for name in [
>     query.get('name1', [''])[0],
>     query.get('name2', [None])[0],
>     query.get('name3', [None])[0],
> ]:
>     if name is not None:
>         continue
>     cookie = cookies.get(name)
>     if cookie:
>         print(...)
>     else:
>         print(...)
> 

Implemented this change


> > LayoutTests/http/tests/storageAccess/resources/get-cookies.py:48
> > +print('<p id="output"></p>')
> 
> I think this is a good use-case for a multi-line string (ie, ''' <content>
> '''
> 
> > LayoutTests/http/tests/storageAccess/resources/set-cookie.py:20
> > +print('<script>')
> 
> Another code case for a multi-line string.

I like these stylistic changes quite a bit. It makes the large blocks of content much cleaner
Comment 6 Jonathan Bedard 2021-02-12 11:27:38 PST
Comment on attachment 420134 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=420134&action=review

> LayoutTests/http/tests/storageAccess/resources/get-cookies.py:29
> +        print('Received cookie named \'{}\'.<br>'.format(name), end='')

Looks like the code I have you wasn't 100% correct, this seems to be what's causing the test failure EWS is flagging.
Comment 7 Chris Gambrell 2021-02-12 12:06:28 PST
Created attachment 420159 [details]
Patch
Comment 8 Chris Gambrell 2021-02-12 12:09:23 PST
(In reply to Jonathan Bedard from comment #6)
> Comment on attachment 420134 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=420134&action=review
> 
> > LayoutTests/http/tests/storageAccess/resources/get-cookies.py:29
> > +        print('Received cookie named \'{}\'.<br>'.format(name), end='')
> 
> Looks like the code I have you wasn't 100% correct, this seems to be what's
> causing the test failure EWS is flagging.

The for loop in the cookie header separation is still required...

for cookie in header_cookies:
        cookie = cookie.split('=')
        cookies[cookie[0]] = cookie[1]

...due to needing to separate...

['foo=bar', 'hello=world']

...into...

{
    'foo': 'bar',
    'hello: 'world'
}

Fixed that in latest patch.
Comment 9 EWS 2021-02-12 14:13:07 PST
Committed r272812: <https://commits.webkit.org/r272812>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 420159 [details].