Skip to content

Commit

Permalink
Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
francescogabbrielli committed Mar 21, 2021
1 parent 9716e05 commit 079441f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Angular2-Xmlrpc

An Angular2+ service which provides XML-RPC communication methods.
An Angular (9+) service which provides XML-RPC communication methods.

This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 11.2.6.
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 11.2.6. Dependencies are requiring Angular 9, but this can probably be easily adapted to anything greater or equal to Angular 2.

This is actually a port from the AngularJS library [Angular-Xmlrpc](https://github.com/ITrust/angular-xmlrpc). **NOTE**: IE support has been dropped altogether.

Expand All @@ -15,7 +15,7 @@ Installation
How to use it ?
---------------

First of all, add in you application a dependency in your module:
First of all, add a dependency in your module:
``` typescript
import { XmlrpcModule } from 'angular2-xmlrpc'

Expand All @@ -27,6 +27,7 @@ import { XmlrpcModule } from 'angular2-xmlrpc'
],
...
})
export class AppModule { }
```
You can use it in your application as any other service, and inject it in the constructor as usual.
``` typescript
Expand All @@ -35,15 +36,15 @@ import { XmlrpcService } from 'angular2-xmlrpc'
constructor(..., private xmlrpc:XmlrpcService, ...) {
}
```
In order to pass parameters, you have to wrap them in an array:
There is only one main method to call an XML-RPC method. In order to pass parameters, you have to wrap them in an array:
``` typescript
let xmlrpcCall = this.xmlrpc.callMethod(
const xmlrpcCall = this.xmlrpc.callMethod(
'http://localhost:8080/xmlrpc/endpoint',
'method-name',
['string-param-1', 1, {'obj-param-3': {val1: 1, val2: 'x'}}]
)
```
Response from the server is an `Observable` and can be parsed to a JS object with the `parseResponse` method:
The XML response from the server is wrapped in an `Observable`, and can be parsed to a JS object with the `parseResponse` method:
``` typescript
xmlrpcCall.subscribe(data => console.log(this.xmlrpc.parseResponse(data)))
```
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "angular2-xmlrpc",
"description": "An Angular service which provides XML-RPC communication methods",
"version": "1.0.0",
"version": "1.0.1",
"author": "Francesco Gabbrielli",
"license": "ISC",
"homepage": "https://github.com/francescogabbrielli/angular2-xmlrpc",
"peerDependencies": {
"@angular/common": "^11.2.6",
"@angular/core": "^11.2.6"
"@angular/common": ">=9",
"@angular/core": ">=9"
},
"dependencies": {
"tslib": "^2.0.0"
Expand Down
65 changes: 51 additions & 14 deletions src/lib/xmlrpc.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing'
import { HttpRequest } from '@angular/common/http'
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'

import { HttpClientTestingModule } from '@angular/common/http/testing'

import { XmlrpcService } from './xmlrpc.service';
import { XmlrpcService } from './xmlrpc.service'

describe('XmlrpcService', () => {
let service: XmlrpcService;
let service: XmlrpcService
let httpTestingController: HttpTestingController

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
]
});
service = TestBed.inject(XmlrpcService);
});
imports: [HttpClientTestingModule],
providers: [XmlrpcService]
})
service = TestBed.inject(XmlrpcService)
httpTestingController = TestBed.inject(HttpTestingController)
})

it('should be created', () => {
expect(service).toBeTruthy();
});
});
expect(service).toBeTruthy()
})

it('should parse XML-RPC response', () => {
const mockResponse =
"<?xml version='1.0'?><methodResponse><params><param><value><int>5</int></value></param></params></methodResponse>"
expect(service.parseResponse(mockResponse)).toEqual(5)
})

it('should return param*param', () => {
const param = Math.floor(Math.random() * Math.floor(10)) + 2
console.log("PARAM", param)
const url = 'http://mockaddress'

const mockResponse =
"<?xml version=\"1.0\"?><methodResponse><params><param><value><int>" +
(param * param) +
"</int></value></param></params></methodResponse>"

service.callMethod(url, 'square', [param]).subscribe(
data => {
console.log("RESPONSE", data)
expect(service.parseResponse(data)).toEqual(param*param)
}
)

const req = httpTestingController.expectOne((hr: HttpRequest<any>) => {
expect(hr.url).toBe(url)
expect(hr.method).toBe('POST')
expect(service.parseResponse(hr.body)).toEqual(param)
return true
})

req.flush(mockResponse)

httpTestingController.verify()
})

})
1 change: 0 additions & 1 deletion src/lib/xmlrpc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export class XmlrpcService {
doc.firstChild!.appendChild(paramsNode)
}
const ret = this.serialize(doc)
console.log(ret)
return ret.replace(/[\s\xa0]+$/, '')
}

Expand Down

0 comments on commit 079441f

Please sign in to comment.