I was looking at the Mercado Libre Java SDK and I noticed that they have a folder called "mockapi" in their repository. It looks like they have written an entire mock api in javascript that they use to test their Java client. However, I still don't quite get how it works. I've posted part of the javascript that is in app.js they are using to do this below.
var express = require('express');
var fs = require('fs');
var app = express.createServer();
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
});
app.post('/oauth/token', function(req, res) {
if(req.query["grant_type"]=="authorization_code") {
if(req.query["code"]=="bad code") {
res.send({"message":"Error validando el parámetro code","error":"invalid_grant","status":400,"cause":[]}, 400);
} else if(req.query["code"]=="valid code without refresh token") {
res.send({
"access_token" : "valid token",
"token_type" : "bearer",
"expires_in" : 10800,
"scope" : "write read"
});
} else if(req.query["code"]=="valid code with refresh token") {
res.send({
"access_token" : "valid token",
"token_type" : "bearer",
"expires_in" : 10800,
"refresh_token" : "valid refresh token",
"scope" : "write read"
});
} else {
res.send(404);
}
} else if(req.query['grant_type']=='refresh_token') {
if(req.query['refresh_token']=='valid refresh token') {
res.send({
"access_token" : "valid token",
"token_type" : "bearer",
"expires_in" : 10800,
"scope" : "write read"
});
}
}
});
app.listen(3000);
fs.writeFileSync('/tmp/mockapi.pid', process.pid);
Also, after looking at their file package.json it looks like they are using node. The file is listed below.
{
"name": "mockapi",
"version": "1.0.0",
"dependencies": {
"express" : "2.5.x"
},
"engine": "node ~> 0.8.x"
}
However, after looking at their pom file, it doesn't look like they have Node as a dependancy.I've done mostly Java development, so server-side Javascript is still foreign to me. How is this working exactly? Why don't they have to include a dependency in their POM file? I should also note that most of the tests are failing because I can't make a connection with localhost:3000. Do I have to have node.js installed to be able to run the tests?
Edit: I've added the Junit test that tests the post below as well as a few other tests
package com.mercadolibre.sdk;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import com.ning.http.client.FluentStringsMap;
import com.ning.http.client.Response;
public class MeliTest extends Assert {
@Test
public void testGetAuthUrl() {
assertEquals(
"https://auth.mercadolibre.com.ar/authorization?response_type=code&client_id=123456&redirect_uri=http%3A%2F%2Fsomeurl.com",
new Meli(123456l, "client secret")
.getAuthUrl("http://someurl.com"));
}
@Test(expected = AuthorizationFailure.class)
public void testAuthorizationFailure() throws AuthorizationFailure {
Meli.apiUrl = "http://localhost:3000";
new Meli(123456l, "client secret").authorize("bad code",
"http://someurl.com");
}
@Test
public void testAuthorizationSuccess() throws AuthorizationFailure {
Meli.apiUrl = "http://localhost:3000";
Meli m = new Meli(123456l, "client secret");
m.authorize("valid code with refresh token", "http://someurl.com");
assertEquals("valid token", m.getAccessToken());
assertEquals("valid refresh token", m.getRefreshToken());
}
@Test
public void testGetWithRefreshToken() throws MeliException, IOException {
Meli.apiUrl = "http://localhost:3000";
Meli m = new Meli(123456l, "client secret", "expired token",
"valid refresh token");
FluentStringsMap params = new FluentStringsMap();
params.add("access_token", m.getAccessToken());
Response response = m.get("/users/me", params);
assertEquals(200, response.getStatusCode());
assertFalse(response.getResponseBody().isEmpty());
}
public void testPost() throws MeliException {
Meli m = new Meli(123456l, "client secret", "valid token");
FluentStringsMap params = new FluentStringsMap();
params.add("access_token", m.getAccessToken());
Response r = m.post("/items", params, "{\"foo\":\"bar\"}");
assertEquals(201, r.getStatusCode());
}