-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpTests.hx
More file actions
151 lines (135 loc) · 4.85 KB
/
HttpTests.hx
File metadata and controls
151 lines (135 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package;
class HttpTests {
var t:Main;
public function new(main:Main) {
t = main;
}
public function run() {
t.section("HttpConstructor");
checkConstructor();
t.section("HttpUrl");
checkUrl();
t.section("HttpSetHeader");
checkSetHeader();
t.section("HttpAddHeader");
checkAddHeader();
t.section("HttpSetParameter");
checkSetParameter();
t.section("HttpAddParameter");
checkAddParameter();
t.section("HttpSetPostData");
checkSetPostData();
t.section("HttpCallbacks");
checkCallbacks();
t.section("HttpRequestNoTransfer");
checkRequestNoTransfer();
t.section("HttpRequestUrl");
checkRequestUrl();
}
function checkConstructor() {
var http = new haxe.Http("http://example.com");
t.stringEquals("http://example.com", http.url, "url set by constructor");
var rd:Dynamic = http.responseData;
t.isTrue(rd == null, "responseData initially null");
}
function checkUrl() {
var http = new haxe.Http("http://first.com");
t.stringEquals("http://first.com", http.url, "url is first value");
http.url = "http://second.com";
t.stringEquals("http://second.com", http.url, "url updated");
}
function checkSetHeader() {
var http = new haxe.Http("http://example.com");
http.setHeader("Content-Type", "application/json");
http.setHeader("Accept", "text/html");
var headers:Dynamic = Reflect.field(http, "headers");
t.intEquals(2, headers.Count(), "two distinct headers");
// Replace existing header
http.setHeader("Content-Type", "text/plain");
headers = Reflect.field(http, "headers");
t.intEquals(2, headers.Count(), "setHeader replaces, still 2");
t.stringEquals("text/plain", headers[0].value, "header value replaced");
}
function checkAddHeader() {
var http = new haxe.Http("http://example.com");
http.addHeader("X-Custom", "val1");
http.addHeader("X-Custom", "val2");
var headers:Dynamic = Reflect.field(http, "headers");
t.intEquals(2, headers.Count(), "addHeader allows duplicates");
}
function checkSetParameter() {
var http = new haxe.Http("http://example.com");
http.setParameter("key1", "val1");
http.setParameter("key2", "val2");
var params:Dynamic = Reflect.field(http, "params");
t.intEquals(2, params.Count(), "two distinct params");
// Replace existing param
http.setParameter("key1", "updated");
params = Reflect.field(http, "params");
t.intEquals(2, params.Count(), "setParameter replaces, still 2");
t.stringEquals("updated", params[0].value, "param value replaced");
}
function checkAddParameter() {
var http = new haxe.Http("http://example.com");
http.addParameter("key", "val1");
http.addParameter("key", "val2");
var params:Dynamic = Reflect.field(http, "params");
t.intEquals(2, params.Count(), "addParameter allows duplicates");
}
function checkSetPostData() {
var http = new haxe.Http("http://example.com");
var pd:Dynamic = Reflect.field(http, "postData");
t.isTrue(pd == null, "postData initially null");
http.setPostData("body content");
pd = Reflect.field(http, "postData");
t.stringEquals("body content", pd, "postData set");
http.setPostData(null);
pd = Reflect.field(http, "postData");
t.isTrue(pd == null, "postData cleared to null");
}
function checkCallbacks() {
var http = new haxe.Http("http://example.com");
var dataCalled = false;
var errorCalled = false;
var statusCode = 0;
http.onData = function(data:String) {
dataCalled = true;
};
http.onError = function(msg:String) {
errorCalled = true;
};
http.onStatus = function(status:Int) {
statusCode = status;
};
var hasOnData:Bool = http.onData != null;
var hasOnError:Bool = http.onError != null;
var hasOnStatus:Bool = http.onStatus != null;
t.isTrue(hasOnData, "onData callback assigned");
t.isTrue(hasOnError, "onError callback assigned");
t.isTrue(hasOnStatus, "onStatus callback assigned");
}
function checkRequestNoTransfer() {
// In the brs emulator, roUrlTransfer is not available
// request() should call onError. Since BRS closures can't capture outer
// local variables, we verify via m (the caller AA) inside the callback.
var http = new haxe.Http("http://example.com");
Reflect.setField(http, "_errorMsg", "");
var errorMsg = "";
http.onError = function(msg:String) {
// When called as self.onError(msg), m refers to self (the http AA)
// untyped __brs__('m._errorMsg = {0}', msg);
errorMsg = msg;
};
http.request();
// var errorMsg:String = Reflect.field(http, "_errorMsg");
var hasError = errorMsg != "";
t.isTrue(hasError, "request calls onError when roUrlTransfer unavailable");
t.stringEquals("roUrlTransfer not available", errorMsg, "error message correct");
}
function checkRequestUrl() {
// In the brs emulator, roUrlTransfer is not available
// requestUrl returns empty string
var result = haxe.Http.requestUrl("http://example.com");
t.stringEquals("", result, "requestUrl returns empty when no roUrlTransfer");
}
}