2012-12-26 11 views
5

Esiste uno strumento per semplificare facilmente il servizio di restful in modo da poter testare facilmente le nostre chiamate Ajax?per semplificare facilmente il servizio restful

Ad esempio, ho bisogno di prendere in giro un servizio riposante per restituire string nel formato JSON o XML.

+0

un possbile duplicare http://stackoverflow.com/questions/203495/testing-rest-webservices/12298362#12298362 – AurA

risposta

1

Prova jmockit; l'avevo usato per prendere in giro un servizio web. Ma questa è una soluzione Java. Se vuoi simulare l'API REST sul lato server, questo si adatterà. Questo non aiuta se non si possiede l'applicazione REST.

Se si vuole prendere in giro lato client (in JS) stesso;

È possibile scrivere il proprio framework/interfaccia di derisione. Quindi, quando si invia una richiesta, inserire un livello intermedio che può solo restituire la risposta al test invece di chiamare effettivamente l'URL REST.

client ---> Mocking Interfaccia ---> REST API CALL

function mockingInterface(var url){ 
    //if original 
    //make REST call 

    //else; return mocked data 
} 
+0

Che cosa succede se non possiedo applicazione REST? Il secondo modo è l'unica soluzione? – blue123

+0

Sì, allora è meglio farlo dal lato client in JS. –

+0

Grazie. Perché non posso io, rai.skumar? – blue123

0

Si può provare http://apiary.io/ troppo.

È possibile definire le risposte della richiesta in formato testo, ad esempio in JSON. Il vantaggio è che l'API MOCK è pubblica, quindi qualsiasi parte del team può utilizzarla.

0

FakeRest fa esattamente quello che vuoi.

// initialize fake REST server and data 
var restServer = new FakeRest.Server(); 
restServer.init({ 
    'authors': [ 
     { id: 0, first_name: 'Leo', last_name: 'Tolstoi' }, 
     { id: 1, first_name: 'Jane', last_name: 'Austen' } 
    ], 
    'books': [ 
     { id: 0, author_id: 0, title: 'Anna Karenina' }, 
     { id: 1, author_id: 0, title: 'War and Peace' }, 
     { id: 2, author_id: 1, title: 'Pride and Prejudice' }, 
     { id: 3, author_id: 1, title: 'Sense and Sensibility' } 
    ] 
}); 
// use sinon.js to monkey-patch XmlHttpRequest 
var server = sinon.fakeServer.create(); 
server.respondWith(restServer.getHandler()); 

// Now query the fake REST server 
var req = new XMLHttpRequest(); 
req.open("GET", "/authors", false); 
req.send(null); 
console.log(req.responseText); 
// [ 
// {"id":0,"first_name":"Leo","last_name":"Tolstoi"}, 
// {"id":1,"first_name":"Jane","last_name":"Austen"} 
// ] 
Problemi correlati