Skip to content

Commit

Permalink
tests: Add test for flash.net.navigateToURL
Browse files Browse the repository at this point in the history
  • Loading branch information
svitkin authored and evilpie committed Feb 12, 2024
1 parent 5028f66 commit b55db70
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 10 deletions.
41 changes: 31 additions & 10 deletions core/src/avm2/globals/flash/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ fn object_to_index_map<'gc>(
Ok(map)
}

fn parse_data<'gc>(
activation: &mut Activation<'_, 'gc>,
url: &String,
data: &Value<'gc>,
) -> Result<(String, IndexMap<String, String>), Error<'gc>> {
let mut url = url.to_string();
let mut vars = IndexMap::new();
let urlvariables = activation
.avm2()
.classes()
.urlvariables
.inner_class_definition();
if data.is_of_type(activation, urlvariables) {
let obj = data.coerce_to_object(activation)?;
vars = object_to_index_map(activation, &obj).unwrap_or_default();
} else {
let str_data = data.coerce_to_string(activation)?.to_string();
if !url.contains('?') {
url.push('?');
}
url.push_str(&str_data);
}
Ok((url, vars))
}

/// Implements `flash.net.navigateToURL`
pub fn navigate_to_url<'gc>(
activation: &mut Activation<'_, 'gc>,
Expand All @@ -63,21 +88,17 @@ pub fn navigate_to_url<'gc>(
2007,
)?)),
url => {
let url = url.coerce_to_string(activation)?;
let url = url.coerce_to_string(activation)?.to_string();
let method = request
.get_public_property("method", activation)?
.coerce_to_string(activation)?;
let method =
NavigationMethod::from_method_str(&method).unwrap();
let data = request
.get_public_property("data", activation)?
.coerce_to_object(activation)?;
// If data is byte array this will not work
let data = object_to_index_map(activation, &data).unwrap_or_default();
let method = NavigationMethod::from_method_str(&method).unwrap();
let data: Value<'gc> = request.get_public_property("data", activation)?;
let (url, vars) = parse_data(activation, &url, &data)?;
activation.context.navigator.navigate_to_url(
&url.to_utf8_lossy(),
&url,
&target.to_utf8_lossy(),
Some((method, data)),
Some((method, vars)),
);
Ok(Value::Undefined)
}
Expand Down
33 changes: 33 additions & 0 deletions tests/tests/swfs/avm2/net_navigateToURL/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package {
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.navigateToURL;
import flash.display.MovieClip;
public class Test extends MovieClip {
public function Test()
{
var request:URLRequest = new URLRequest("https://example.com/purchase/");
var variables:URLVariables = new URLVariables();
variables.sku = "Test";
var empty_variables:URLVariables = new URLVariables();
var cases:Array = [
["POST", empty_variables],
["GET", empty_variables],
["POST", variables],
["GET", variables],
["POST", "sku=Test"],
["GET", "sku=Test"]
];
for each (var case_tuple:Array in cases) {
var method:String = case_tuple[0];
var data:Object = case_tuple[1];
request.method = method;
request.data = data;
trace("// Method:", method);
trace("// Data:", data);
navigateToURL(request, "_blank");
trace("");
}
}
}
}
44 changes: 44 additions & 0 deletions tests/tests/swfs/avm2/net_navigateToURL/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Method: POST
// Data:
Navigator::navigate_to_url:
URL: https://example.com/purchase/
Target: _blank
Method: POST

// Method: GET
// Data:
Navigator::navigate_to_url:
URL: https://example.com/purchase/
Target: _blank
Method: GET

// Method: POST
// Data: sku=Test
Navigator::navigate_to_url:
URL: https://example.com/purchase/
Target: _blank
Method: POST
Param: sku=Test

// Method: GET
// Data: sku=Test
Navigator::navigate_to_url:
URL: https://example.com/purchase/
Target: _blank
Method: GET
Param: sku=Test

// Method: POST
// Data: sku=Test
Navigator::navigate_to_url:
URL: https://example.com/purchase/?sku=Test
Target: _blank
Method: POST

// Method: GET
// Data: sku=Test
Navigator::navigate_to_url:
URL: https://example.com/purchase/?sku=Test
Target: _blank
Method: GET

Binary file added tests/tests/swfs/avm2/net_navigateToURL/test.swf
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/tests/swfs/avm2/net_navigateToURL/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
num_frames = 1
log_fetch = true

0 comments on commit b55db70

Please sign in to comment.