From 079441f2b6b59f445372c86d21638e2cba42e1bd Mon Sep 17 00:00:00 2001 From: Francesco Gabbrielli Date: Mon, 22 Mar 2021 00:07:54 +0100 Subject: [PATCH] Testing --- README.md | 13 +++---- package.json | 6 ++-- src/lib/xmlrpc.service.spec.ts | 65 ++++++++++++++++++++++++++-------- src/lib/xmlrpc.service.ts | 1 - 4 files changed, 61 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 42f1d86..e541e31 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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' @@ -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 @@ -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))) ``` diff --git a/package.json b/package.json index c02e6c3..97d43fb 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/lib/xmlrpc.service.spec.ts b/src/lib/xmlrpc.service.spec.ts index 5289b76..0299702 100644 --- a/src/lib/xmlrpc.service.spec.ts +++ b/src/lib/xmlrpc.service.spec.ts @@ -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 = + "5" + 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 = + "" + + (param * param) + + "" + + service.callMethod(url, 'square', [param]).subscribe( + data => { + console.log("RESPONSE", data) + expect(service.parseResponse(data)).toEqual(param*param) + } + ) + + const req = httpTestingController.expectOne((hr: HttpRequest) => { + expect(hr.url).toBe(url) + expect(hr.method).toBe('POST') + expect(service.parseResponse(hr.body)).toEqual(param) + return true + }) + + req.flush(mockResponse) + + httpTestingController.verify() + }) + +}) diff --git a/src/lib/xmlrpc.service.ts b/src/lib/xmlrpc.service.ts index 59ca244..f7c9b34 100644 --- a/src/lib/xmlrpc.service.ts +++ b/src/lib/xmlrpc.service.ts @@ -45,7 +45,6 @@ export class XmlrpcService { doc.firstChild!.appendChild(paramsNode) } const ret = this.serialize(doc) - console.log(ret) return ret.replace(/[\s\xa0]+$/, '') }