Repository: jquery/jquery
File: test/unit/ajax.js (JavaScript)

Code before:
	QUnit.test( "jQuery[get|post]( options ) - simple with xml", function( assert ) {
		assert.expect( 2 );
		var done = assert.async();

		jQuery.when.apply( jQuery,
			jQuery.map( [ "get", "post" ], function( method ) {
				return jQuery[ method ]( {
					url: url( "mock.php?action=xml" ),
					data: {
						cal: "5-2"
					},
					success: function( xml ) {
						jQuery( "math", xml ).each( function() {
							assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
						} );
					}
				} );
			} )
		).always( function() {
			done();
		} );
	} );

	// We need to simulate cross-domain requests with the feature that both 127.0.0.1 and localhost point to the mock http server
	// So skip the the test if we are not in localhost.
	QUnit[ ( !QUnit.isSwarm || location.hostname === "localhost" ) ? "test" : "skip" ](
		"jQuery.append with crossorigin attribute", function( assert ) {
		assert.expect( 1 );

		var done = assert.async(),
				timeout;

		Globals.register( "corsCallback" );
		window.corsCallback = function( response ) {
			assert.ok( response === true, "Callback fired" );
			window.clearTimeout( timeout );
			done();
		};

		var src = baseURL + "mock.php?action=cors";

		// Simulate a cross-origin request.
		// Full cross-origin verification is only done when tests run from localhost,
		// e.g. when run via Karma (as we can depend on localhost resolving to 127.0.0.1).
		// In other cases we only do a more limited check: that the Origin header is sent.
		src = src.replace( "localhost", "127.0.0.1" );
		var html = "<script type=\"text/javascript\" src=\"" + src + "\" crossorigin=\"anonymous\"><\/script>";

		jQuery( document.body ).append( html );
		timeout = window.setTimeout( function() {
			assert.ok( false, "Callback should have fired" );

Review comment (bug):
This test will fail in some browsers (e.g. Firefox) when run locally without TestSwarm but without a Node.js-based setup, e.g. with a custom domain for jQuery because no replacement of `localhost` to `127.0.0.1` will then work.

We may need to detect that we're in the Node.js server and only force-run the test then, the `QUnit.isSwarm` check is not enough then.

I'd suggest checking `window.__karma__` instead of `!QUnit.isSwarm` here.

Code after:
					assert.strictEqual( data, "test%5Blength%5D=7&test%5Bfoo%5D=bar", "Check if a sub-object with a length param is serialized correctly" );
				}
			} )
		).always( done );
	} );

	QUnit.test( "jQuery.post( String, Hash, Function ) - simple with xml", function( assert ) {
		assert.expect( 4 );
		var done = assert.async();

		jQuery.when(
			jQuery.post(
				url( "mock.php?action=xml" ),
				{
					cal: "5-2"
				},
				function( xml ) {
					jQuery( "math", xml ).each( function() {
						assert.strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
						assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
					} );
				}
			),
			jQuery.post( url( "mock.php?action=xml&cal=5-2" ), {}, function( xml ) {
				jQuery( "math", xml ).each( function() {
					assert.strictEqual( jQuery( "calculation", this ).text(), "5-2", "Check for XML" );
					assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
				} );
			} )
		).always( function() {
			done();
		} );
	} );

	QUnit.test( "jQuery[get|post]( options ) - simple with xml", function( assert ) {
		assert.expect( 2 );
		var done = assert.async();

		jQuery.when.apply( jQuery,
			jQuery.map( [ "get", "post" ], function( method ) {
				return jQuery[ method ]( {
					url: url( "mock.php?action=xml" ),
					data: {
						cal: "5-2"
					},
					success: function( xml ) {
						jQuery( "math", xml ).each( function() {
							assert.strictEqual( jQuery( "result", this ).text(), "3", "Check for XML" );
						} );
					}
				} );