Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

2.5 KB
Raw Blame
import pytest
from pytest_httpserver import HTTPServer
from werkzeug import Response

from browser_use.llm.aws.serializer import AWSBedrockMessageSerializer
from browser_use.llm.messages import ContentPartImageParam, ImageURL


@pytest.mark.parametrize(
	'url',
	[
		'https://cdn.example.com/photo.jpg',
		'https://cdn.example.com/photo.jpg?width=800',
		'https://cdn.example.com/photo.jpg#preview',
		'https://cdn.example.com/photo.jpg?width=800#preview',
		'HTTPS://cdn.example.com/photo.jpg',
		'https://cdn.example.com/photo.PNG?width=800',
	],
)
def test_is_url_image_accepts_supported_http_urls(url: str) -> None:
	assert AWSBedrockMessageSerializer._is_url_image(url)


@pytest.mark.parametrize(
	'url',
	[
		'ftp://cdn.example.com/photo.jpg',
		'data:image/png;base64,aGVsbG8=',
		'https://cdn.example.com/photo.svg',
		'https://cdn.example.com/photo.bmp',
		'https://cdn.example.com/photo.bmp?signature=example',
		'https://cdn.example.com/photo?format=.jpg',
		'https:photo.jpg',
		'https:///photo.jpg',
		'https://[::1/photo.jpg',
		'https://[not-an-ipv6-address]/photo.jpg',
		'https://cdn.example.com:invalid/photo.jpg',
	],
)
def test_is_url_image_rejects_unsupported_urls(url: str) -> None:
	assert not AWSBedrockMessageSerializer._is_url_image(url)


def test_serialize_content_part_prefers_response_content_type(httpserver: HTTPServer) -> None:
	image_bytes = b'image-bytes'
	httpserver.expect_request('/photo.jpg').respond_with_data(
		image_bytes,
		content_type='image/png',
	)
	url = httpserver.url_for('/photo.jpg')

	result = AWSBedrockMessageSerializer._serialize_content_part_image(ContentPartImageParam(image_url=ImageURL(url=url)))

	assert result == {
		'image': {
			'format': 'png',
			'source': {
				'bytes': image_bytes,
			},
		}
	}


@pytest.mark.parametrize('content_type', ['application/octet-stream', None])
def test_serialize_content_part_uses_url_path_when_content_type_is_unavailable(
	httpserver: HTTPServer,
	content_type: str | None,
) -> None:
	image_bytes = b'image-bytes'
	response = Response(image_bytes, content_type=content_type)
	if content_type is None:
		response.headers.pop('Content-Type', None)
	httpserver.expect_request('/photo.png', query_string='signature=example').respond_with_response(response)
	url = f'{httpserver.url_for("/photo.png")}?signature=example'.replace('http://', 'HTTP://', 1)

	result = AWSBedrockMessageSerializer._serialize_content_part_image(ContentPartImageParam(image_url=ImageURL(url=url)))

	assert result == {
		'image': {
			'format': 'png',
			'source': {
				'bytes': image_bytes,
			},
		}
	}