@@ -201,6 +201,46 @@ def test_image_block_resolve_image_url(png_1px_b64: bytes, png_1px: bytes):
201
201
assert img .read () == png_1px_b64
202
202
203
203
204
+ def test_image_block_resolve_image_data_url_base64 (png_1px_b64 : bytes , png_1px : bytes ):
205
+ # Test data URL with base64 encoding
206
+ data_url = f"data:image/png;base64,{ png_1px_b64 .decode ('utf-8' )} "
207
+ b = ImageBlock (url = AnyUrl (url = data_url ))
208
+
209
+ img = b .resolve_image ()
210
+ assert isinstance (img , BytesIO )
211
+ assert img .read () == png_1px
212
+
213
+ img = b .resolve_image (as_base64 = True )
214
+ assert isinstance (img , BytesIO )
215
+ assert img .read () == png_1px_b64
216
+
217
+
218
+ def test_image_block_resolve_image_data_url_plain_text ():
219
+ # Test data URL with plain text (no base64)
220
+ test_text = "Hello, World!"
221
+ data_url = f"data:text/plain,{ test_text } "
222
+ b = ImageBlock (url = AnyUrl (url = data_url ))
223
+
224
+ img = b .resolve_image ()
225
+ assert isinstance (img , BytesIO )
226
+ assert img .read () == test_text .encode ("utf-8" )
227
+
228
+ img = b .resolve_image (as_base64 = True )
229
+ assert isinstance (img , BytesIO )
230
+ assert img .read () == base64 .b64encode (test_text .encode ("utf-8" ))
231
+
232
+
233
+ def test_image_block_resolve_image_data_url_invalid ():
234
+ # Test invalid data URL format (missing comma)
235
+ invalid_data_url = "data:image/png;base64"
236
+ b = ImageBlock (url = AnyUrl (url = invalid_data_url ))
237
+
238
+ with pytest .raises (
239
+ ValueError , match = "Invalid data URL format: missing comma separator"
240
+ ):
241
+ b .resolve_image ()
242
+
243
+
204
244
def test_image_block_resolve_error ():
205
245
with pytest .raises (
206
246
ValueError , match = "No valid source provided to resolve binary data!"
@@ -383,6 +423,20 @@ def test_video_block_resolve_video_url(mp4_bytes: bytes, mp4_base64: bytes):
383
423
assert vid .read () == mp4_base64
384
424
385
425
426
+ def test_video_block_resolve_video_data_url_base64 (mp4_bytes : bytes , mp4_base64 : bytes ):
427
+ # Test data URL with base64 encoding
428
+ data_url = f"data:video/mp4;base64,{ mp4_base64 .decode ('utf-8' )} "
429
+ b = VideoBlock (url = AnyUrl (url = data_url ))
430
+
431
+ vid = b .resolve_video ()
432
+ assert isinstance (vid , BytesIO )
433
+ assert vid .read () == mp4_bytes
434
+
435
+ vid = b .resolve_video (as_base64 = True )
436
+ assert isinstance (vid , BytesIO )
437
+ assert vid .read () == mp4_base64
438
+
439
+
386
440
def test_video_block_resolve_error ():
387
441
b = VideoBlock ()
388
442
with pytest .raises (ValueError , match = "No valid source provided" ):
0 commit comments