Skip to content

URLField serializer is not playing nice with "built in" test server address http://testserver #9704

Closed
@yarikoptic

Description

@yarikoptic

Initially encountered in our

(Pdb) p resp.content
b'{"schema_url":["Enter a valid URL."]}'

with such minimal reproducer depicting the issue:

from rest_framework import serializers

class MySerializer(serializers.Serializer):
    url = serializers.URLField()

def test_valid_url():
    serializer = MySerializer(data={'url': 'http://testserver/path'})
    serializer.is_valid()

running pytest on which would give you an ugly excepted demanding configuration since i18n would not yet be configured to give a proper localized error...

but replacing that simple hostname testserver with a domain name with the TLD (e.g. testserver.com), which is AFAIK is not required for a URL to be valid, pytest would pass just fine.

❯ sed -i -E 's,(testserver),\1.com,g' /tmp/tttt.py
❯ python -m pytest -s -v /tmp/tttt.py
====================================================================================================== test session starts =======================================================================================================
platform linux -- Python 3.11.12, pytest-8.3.5, pluggy-1.6.0 -- /home/yoh/proj/dandi/dandi-archive/.venv/bin/python
cachedir: .pytest_cache
django: version: 4.2.21
rootdir: /tmp
plugins: django-s3-file-field-1.0.1, Faker-37.3.0, mock-3.14.0, memray-1.7.0, factoryboy-2.7.0, django-4.11.1, cov-6.1.1
collected 1 item                                                                                                                                                                                                                 

../../../../../tmp/tttt.py::test_valid_url PASSED

So

  • any way to workaround?
  • if I got it right such validation stems from DJANGO itself, so should it be checked/fixed there?

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