Skip to content

Commit 5186b37

Browse files
author
Sefa Ilkimen
committed
feature silkimen#34: add support for transfering raw string as utf-8 encoded data
2 parents c7820c8 + d28eede commit 5186b37

File tree

12 files changed

+585
-367
lines changed

12 files changed

+585
-367
lines changed

plugin.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
<dependency id="cordova-plugin-file" version=">=2.0.0"/>
1111
<js-module src="www/lodash.js" name="lodash"/>
1212
<js-module src="www/umd-tough-cookie.js" name="tough-cookie"/>
13+
<js-module src="www/messages.js" name="messages"/>
1314
<js-module src="www/local-storage-store.js" name="local-storage-store"/>
1415
<js-module src="www/cookie-handler.js" name="cookie-handler"/>
1516
<js-module src="www/angular-integration.js" name="angular-integration"/>
17+
<js-module src="www/helpers.js" name="helpers"/>
1618
<js-module src="www/advanced-http.js" name="http">
1719
<clobbers target="cordova.plugin.http"/>
1820
</js-module>
@@ -24,6 +26,7 @@
2426
</config-file>
2527
<header-file src="src/ios/CordovaHttpPlugin.h"/>
2628
<header-file src="src/ios/TextResponseSerializer.h"/>
29+
<header-file src="src/ios/TextRequestSerializer.h"/>
2730
<header-file src="src/ios/AFNetworking/AFHTTPSessionManager.h"/>
2831
<header-file src="src/ios/AFNetworking/AFNetworking.h"/>
2932
<header-file src="src/ios/AFNetworking/AFNetworkReachabilityManager.h"/>
@@ -33,6 +36,7 @@
3336
<header-file src="src/ios/AFNetworking/AFURLSessionManager.h"/>
3437
<source-file src="src/ios/CordovaHttpPlugin.m"/>
3538
<source-file src="src/ios/TextResponseSerializer.m"/>
39+
<source-file src="src/ios/TextRequestSerializer.m"/>
3640
<source-file src="src/ios/AFNetworking/AFHTTPSessionManager.m"/>
3741
<source-file src="src/ios/AFNetworking/AFNetworkReachabilityManager.m"/>
3842
<source-file src="src/ios/AFNetworking/AFSecurityPolicy.m"/>
@@ -64,4 +68,4 @@
6468
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPatch.java" target-dir="src/com/synconset/cordovahttp"/>
6569
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpUpload.java" target-dir="src/com/synconset/cordovahttp"/>
6670
</platform>
67-
</plugin>
71+
</plugin>

src/android/com/synconset/cordovahttp/CordovaHttp.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ protected HttpRequest setupDataSerializer(HttpRequest request) throws JSONExcept
144144
if (new String("json").equals(this.getSerializerName())) {
145145
request.contentType(request.CONTENT_TYPE_JSON, request.CHARSET_UTF8);
146146
request.send(this.getParamsObject().toString());
147-
} else {
147+
} else if (new String("utf8").equals(this.getSerializerName())) {
148+
request.contentType("text/plain", request.CHARSET_UTF8);
149+
request.send(this.getParamsMap().get("text").toString());
150+
} else
151+
{
148152
request.form(this.getParamsMap());
149153
}
150154

src/ios/CordovaHttpPlugin.m

Lines changed: 221 additions & 162 deletions
Large diffs are not rendered by default.

src/ios/TextRequestSerializer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#import <Foundation/Foundation.h>
2+
#import "AFURLRequestSerialization.h"
3+
4+
@interface TextRequestSerializer : AFHTTPRequestSerializer
5+
6+
+ (instancetype)serializer;
7+
8+
@end

src/ios/TextRequestSerializer.m

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#import "TextRequestSerializer.h"
2+
3+
@implementation TextRequestSerializer
4+
5+
+ (instancetype)serializer
6+
{
7+
TextRequestSerializer *serializer = [[self alloc] init];
8+
return serializer;
9+
}
10+
11+
#pragma mark - AFURLRequestSerialization
12+
13+
- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
14+
withParameters:(id)parameters
15+
error:(NSError *__autoreleasing *)error
16+
{
17+
NSParameterAssert(request);
18+
19+
if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
20+
return [super requestBySerializingRequest:request withParameters:parameters error:error];
21+
}
22+
23+
NSMutableURLRequest *mutableRequest = [request mutableCopy];
24+
25+
[self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) {
26+
if (![request valueForHTTPHeaderField:field]) {
27+
[mutableRequest setValue:value forHTTPHeaderField:field];
28+
}
29+
}];
30+
31+
if (parameters) {
32+
if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) {
33+
[mutableRequest setValue:@"text/plain; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
34+
}
35+
36+
[mutableRequest setHTTPBody: [[parameters valueForKey:@"text"] dataUsingEncoding:NSUTF8StringEncoding]];
37+
}
38+
39+
return mutableRequest;
40+
}
41+
42+
#pragma mark - NSSecureCoding
43+
44+
- (instancetype)initWithCoder:(NSCoder *)decoder {
45+
self = [super initWithCoder:decoder];
46+
if (!self) {
47+
return nil;
48+
}
49+
50+
return self;
51+
}
52+
53+
@end

test/app-test-definitions.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const hooks = {
88
const helpers = {
99
acceptAllCerts: function(done) { cordova.plugin.http.acceptAllCerts(true, done, done); },
1010
setJsonSerializer: function(done) { done(cordova.plugin.http.setDataSerializer('json')); },
11+
setUtf8StringSerializer: function(done) { done(cordova.plugin.http.setDataSerializer('utf8')); },
1112
setUrlEncodedSerializer: function(done) { done(cordova.plugin.http.setDataSerializer('urlencoded')); },
1213
getWithXhr: function(done, url) {
1314
var xhr = new XMLHttpRequest();
@@ -382,6 +383,17 @@ const tests = [
382383
cookies.myCookie.should.be.equal('myValue');
383384
cookies.mySecondCookie.should.be.equal('mySecondValue');
384385
}
386+
},{
387+
description: 'should send UTF-8 encoded raw string correctly (POST)',
388+
expected: 'resolved: {"status": 200, "data": "{\\"data\\": \\"this is a test string\\"...',
389+
before: helpers.setUtf8StringSerializer,
390+
func: function(resolve, reject) {
391+
cordova.plugin.http.post('http://httpbin.org/anything', 'this is a test string', {}, resolve, reject);
392+
},
393+
validationFunc: function(driver, result) {
394+
result.type.should.be.equal('resolved');
395+
JSON.parse(result.data.data).data.should.eql('this is a test string');
396+
}
385397
}
386398
];
387399

test/js-mocha-specs.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ const mock = require('mock-require');
33
const path = require('path');
44

55
const should = chai.should();
6+
7+
const HELPERS_ID = path.resolve(__dirname, '..', 'www', 'helpers');
68
const PLUGIN_ID = path.resolve(__dirname, '..', 'www', 'advanced-http');
79

810
describe('Advanced HTTP www interface', function() {
911
let http = {};
12+
let helpers = {};
13+
1014
const noop = () => { /* intentionally doing nothing */ };
15+
1116
const loadHttp = () => {
17+
mock(`${PLUGIN_ID}.helpers`, mock.reRequire('../www/helpers'));
1218
http = mock.reRequire('../www/advanced-http');
1319
};
1420

@@ -19,8 +25,12 @@ describe('Advanced HTTP www interface', function() {
1925
global.btoa = decoded => new Buffer(decoded).toString('base64');
2026

2127
mock('cordova/exec', noop);
22-
mock(`${PLUGIN_ID}.angular-integration`, { registerService: noop });
2328
mock(`${PLUGIN_ID}.cookie-handler`, {});
29+
mock(`${HELPERS_ID}.cookie-handler`, {});
30+
mock(`${PLUGIN_ID}.messages`, require('../www/messages'));
31+
mock(`${HELPERS_ID}.messages`, require('../www/messages'));
32+
mock(`${PLUGIN_ID}.angular-integration`, { registerService: noop });
33+
2434
loadHttp();
2535
});
2636

@@ -40,7 +50,7 @@ describe('Advanced HTTP www interface', function() {
4050
});
4151

4252
it('resolves global headers correctly #24', () => {
43-
mock(`${PLUGIN_ID}.cookie-handler`, {
53+
mock(`${HELPERS_ID}.cookie-handler`, {
4454
getCookieString: () => 'fakeCookieString'
4555
});
4656

@@ -59,7 +69,7 @@ describe('Advanced HTTP www interface', function() {
5969
});
6070

6171
it('resolves host headers correctly (set without port number) #37', () => {
62-
mock(`${PLUGIN_ID}.cookie-handler`, {
72+
mock(`${HELPERS_ID}.cookie-handler`, {
6373
getCookieString: () => 'fakeCookieString'
6474
});
6575

@@ -78,7 +88,7 @@ describe('Advanced HTTP www interface', function() {
7888
});
7989

8090
it('resolves host headers correctly (set with port number) #37', () => {
81-
mock(`${PLUGIN_ID}.cookie-handler`, {
91+
mock(`${HELPERS_ID}.cookie-handler`, {
8292
getCookieString: () => 'fakeCookieString'
8393
});
8494

@@ -97,7 +107,7 @@ describe('Advanced HTTP www interface', function() {
97107
});
98108

99109
it('resolves request headers correctly', () => {
100-
mock(`${PLUGIN_ID}.cookie-handler`, {
110+
mock(`${HELPERS_ID}.cookie-handler`, {
101111
getCookieString: () => 'fakeCookieString'
102112
});
103113

0 commit comments

Comments
 (0)