cancel
Showing results for 
Search instead for 
Did you mean: 

Unittest upload file webscript

bartvandenabeel
Champ in-the-making
Champ in-the-making
Hi,

I want to write a unittest (junit) for testing a webscript that uploads a file (alfresco 3.4.7 enterprise). I use the BaseWebScriptTest as in the documentation. I don't know how i can upload the file with the PostRequest. Is it even possible to do this? It would be nice if we could just use the methods from httpclient to compose our requests for the tests (ex. org.apache.commons.httpclient.methods.PostMethod)

Now i'm getting the next excepiton :
Caused by: org.alfresco.scripts.ScriptException: 01020002 Failed to execute script 'classpath*:alfresco/extension/templates/webscripts/com/amplexor/dsd/process.post.js': 01020001 ReferenceError: "formdata" is not defined. (file:/home/bart/workspace/dsd/alfresco/target/classes/alfresco/extension/templates/webscripts/com/amplexor/dsd/process.post.js#4)


// start class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:system-test-context.xml")
public class UploadWebscriptTest extends BaseWebScriptTest{

// test code

        PostRequest post = new PostRequest("/dsd/sd/process", FileUtils.readFileToByteArray(f), "application/octet-stream");
        Map<String, String> map = new HashMap<String, String>();
        map.put("Content-Disposition", "form-data; name=\"file\"; filename=\"dsd\"");
        map.put("Content-Transfer-Encoding", "binary");
        post.setHeaders(map);
        post.setEncoding("multipart/form-data");
        sendRequest(post, 200);


grtz,
Bart
4 REPLIES 4

mrogers
Star Contributor
Star Contributor
The problem is that you need to run a HTTP server to receive the request, so its not as simple as first it seems.

However there are many of Alfresco's automated tests that call web scrips via Post, use them as a template.

Take a look at the Remote API project, for example SiteServiceTest.

bartvandenabeel
Champ in-the-making
Champ in-the-making
Is it really necessary? When i run a test with org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest it runs perfectly .


Request get = new GetRequest("/dsd/id/process");
Response response = sendRequest(get, 200);

bartvandenabeel
Champ in-the-making
Champ in-the-making
I've looked at the class : http://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/projects/remote-api/source/jav... . It only show how to post Json-messages. That is not a problem. I want to attach a file to my PostMethod. I can't figure out how to do this. Or is is just not possible to do this via the BaseWebScriptTest? When de test starts, does it use a certain port that i can adress? Than i can use HttpClient to post it.

aitbenmouh
Champ in-the-making
Champ in-the-making
When im searching for an exemple of a webscript unitTest i found this : http://wiki.alfresco.com/wiki/3.0_Web_Scripts_Testing but the problem that's not clair.