Skip to content

Support unittest.mock.ANY in header comparison #2439

@dimaqq

Description

@dimaqq

I'm testing a starlette app, let's pretend it's httpbin:

async def test_no_auth(client, services):                                               
    r = client.get("/bearer")                                                           
    assert r.status_code == 401                                                         
    assert r.headers == {                                                         
        "access-control-allow-origin": "*",                                             
        "access-control-allow-methods": "*",                                            
        "access-control-max-age": "86400",                                              
        "content-type": ANY,                                                            
        "x-content-type-options": ANY,                                                  
    }     

This fails with E AttributeError: '_ANY' object has no attribute 'encode'

For now I work around this by "casting" the magic header dict to an actual dict:

async def test_no_auth(client, services):                                               
    r = client.get("/bearer")                                                           
    assert r.status_code == 401                                                         
    assert dict(r.headers) == {                                                         
        "access-control-allow-origin": "*",                                             
        "access-control-allow-methods": "*",                                            
        "access-control-max-age": "86400",                                              
        "content-type": ANY,                                                            
        "x-content-type-options": ANY,                                                  
    }     

Then the comparison works and test is happy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions