Skip to content

Instantly share code, notes, and snippets.

@imvision
Created June 28, 2014 07:28
Show Gist options
  • Save imvision/08a8c4a7e7b35ac6b541 to your computer and use it in GitHub Desktop.
Save imvision/08a8c4a7e7b35ac6b541 to your computer and use it in GitHub Desktop.
Simple web service API client to run tests on all methods of web service
var client = {
endpoint : 'http://localhost/schlumberjer/xpris/service/api.php',
container : $('#tests table'),
request_payload : {},
auth_token : '',
Test : function( id ) {
this.payloadBuild( 'api', 'test' );
this.Send( id );
},
Test_fail : function( id ) {
this.payloadBuild( 'api', 'test_fail' );
this.Send( id );
},
deleteUser : function ( id ) {
this.payloadBuild( 'api', 'deleteUser' );
this.payloadBuild( 'email', '[email protected]' );
this.Send( id );
},
// Authenticate using API
Authentication : function( id ) {
this.payloadBuild( 'api', 'Authentication' );
this.payloadBuild( 'email', '[email protected]' );
this.payloadBuild( 'password', '123456' );
this.Send( id );
},
// Register
Register : function( id ) {
this.payloadBuild( 'api', 'SignUp' );
this.payloadBuild( 'email', '[email protected]' );
this.payloadBuild( 'password', '123456' );
this.payloadBuild( 'name', 'Vision Specialist' );
this.Send( id );
},
// ForgotPassword
ForgotPassword : function() {
},
//
AddRow : function( id, expected ) {
// $(this.container).append( '<tr id="'+id+'"></tr>' );
// $('#' + id).append( '<td>'+ expected +'</td>' );
},
payloadBuild : function( key, val ) {
this.request_payload[key] = val;
},
payloadReset : function () {
this.request_payload = { api : 'test' };
if( this.auth_token != "" ) {
this.request_payload['auth_token'] = this.auth_token;
}
},
Send : function( elem_id ) {
$.ajax({
type: "POST",
url: this.endpoint,
data: this.request_payload,
dataType: 'json'
})
.done(function( msg ) {
var row_id = '#' + elem_id;
$( row_id ).append( '<td>'+msg.result.status+'</td><td>'+msg.result.message+'</td><td>'+JSON.stringify(msg)+'</td>' );
if( $( row_id + ' td:nth-child(2)' ).text() == $( row_id + ' td:nth-child(3)' ).text() ) {
$( row_id ).addClass( 'success' );
} else {
$( row_id ).addClass( 'danger' );
}
if( msg.result.hasOwnProperty( 'auth_token' ) ) {
client.auth_token = msg.result.auth_token;
client.payloadReset();
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment