@@ -770,3 +770,84 @@ def test_load_detects_file_vs_url() -> None:
770770 # Restore original methods
771771 Vcon .load_from_file = original_load_from_file
772772 Vcon .load_from_url = original_load_from_url
773+
774+
775+ def test_save_to_file (tmp_path ):
776+ """Test saving a vCon to a file"""
777+ # Create a vCon with known content
778+ vcon = Vcon .build_from_json (test_vcon_string )
779+
780+ # Save to a temporary file
781+ file_path = tmp_path / "saved_vcon.json"
782+ vcon .save_to_file (str (file_path ))
783+
784+ # Verify the file exists and contains correct content
785+ assert file_path .exists ()
786+ with open (file_path , 'r' ) as f :
787+ saved_content = f .read ()
788+ assert json .loads (saved_content ) == json .loads (vcon .to_json ())
789+
790+
791+ def test_save_to_file_permission_error (tmp_path ):
792+ """Test saving to a file with no write permissions raises IOError"""
793+ vcon = Vcon .build_new ()
794+ file_path = tmp_path / "readonly.json"
795+
796+ # Create a read-only directory
797+ file_path .parent .chmod (0o444 )
798+
799+ with pytest .raises (IOError ):
800+ vcon .save_to_file (str (file_path ))
801+
802+
803+ @pytest .mark .vcr ()
804+ def test_post_to_url ():
805+ """Test posting a vCon to a URL"""
806+ vcon = Vcon .build_new ()
807+ url = "https://httpbin.org/post" # Test endpoint that echoes back the request
808+
809+ # Test with custom headers
810+ headers = {
811+ 'x-conserver-api-token' : 'test-token' ,
812+ 'x-custom-header' : 'test-value'
813+ }
814+
815+ response = vcon .post_to_url (url , headers = headers )
816+
817+ # Verify the response
818+ assert response .status_code == 200
819+ response_data = response .json ()
820+
821+ # Verify the sent data matches our vCon
822+ assert json .loads (response_data ['data' ]) == json .loads (vcon .to_json ())
823+
824+ # Verify headers were sent correctly
825+ assert response_data ['headers' ]['Content-Type' ] == 'application/json'
826+ assert response_data ['headers' ]['X-Conserver-Api-Token' ] == 'test-token'
827+ assert response_data ['headers' ]['X-Custom-Header' ] == 'test-value'
828+
829+
830+ @pytest .mark .vcr ()
831+ def test_post_to_url_no_headers ():
832+ """Test posting a vCon to a URL without custom headers"""
833+ vcon = Vcon .build_new ()
834+ url = "https://httpbin.org/post"
835+
836+ response = vcon .post_to_url (url )
837+
838+ assert response .status_code == 200
839+ response_data = response .json ()
840+
841+ # Verify only default headers were sent
842+ assert response_data ['headers' ]['Content-Type' ] == 'application/json'
843+ assert 'X-Conserver-Api-Token' not in response_data ['headers' ]
844+
845+
846+ @pytest .mark .vcr ()
847+ def test_post_to_url_error ():
848+ """Test posting to an invalid URL raises RequestException"""
849+ vcon = Vcon .build_new ()
850+ url = "https://nonexistent.example.com"
851+
852+ with pytest .raises (requests .RequestException ):
853+ vcon .post_to_url (url )
0 commit comments