Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.tradefed.util.net;
     18 
     19 import com.android.tradefed.util.net.IHttpHelper.DataSizeException;
     20 
     21 import junit.framework.TestCase;
     22 
     23 import org.easymock.EasyMock;
     24 
     25 import java.io.File;
     26 import java.io.FileOutputStream;
     27 import java.io.IOException;
     28 import java.io.PrintWriter;
     29 
     30 /**
     31  * Unit tests for {@link HttpMultipartPost}.
     32  */
     33 public class HttpMultipartPostTest extends TestCase {
     34 
     35     private File mFile;
     36     private IHttpHelper mHelper;
     37 
     38     private static final String CRLF = "\r\n";
     39 
     40     @Override
     41     public void setUp() throws Exception {
     42         super.setUp();
     43 
     44         mFile = File.createTempFile("http-multipart-test", null);
     45         PrintWriter writer = new PrintWriter(new FileOutputStream(mFile));
     46         writer.write("Test data");
     47         writer.close();
     48 
     49         mHelper = EasyMock.createMock(IHttpHelper.class);
     50     }
     51 
     52     public void testSendRequest() throws IOException, DataSizeException {
     53         String expectedContents = "--xXxXx" + CRLF
     54                 + "Content-Disposition: form-data; name=\"param1\"" + CRLF
     55                 + CRLF + "value1" + CRLF + "--xXxXx" + CRLF
     56                 + "Content-Disposition: form-data; name=\"param2\";filename=\""
     57                 + mFile.getAbsolutePath() + "\"" + CRLF
     58                 + "Content-Type: text/plain" + CRLF
     59                 + CRLF + "Test data"
     60                 + CRLF + "--xXxXx--" + CRLF + CRLF;
     61         EasyMock.expect(mHelper.doPostWithRetry("www.example.com", expectedContents,
     62                 "multipart/form-data;boundary=xXxXx")).andReturn("OK");
     63         EasyMock.replay(mHelper);
     64 
     65         HttpMultipartPost testPost = new HttpMultipartPost("www.example.com", mHelper);
     66         testPost.addParameter("param1", "value1");
     67         testPost.addTextFile("param2", mFile);
     68 
     69         testPost.send();
     70 
     71         EasyMock.verify(mHelper);
     72     }
     73 
     74     @Override
     75     public void tearDown() throws Exception {
     76         mFile.delete();
     77         super.tearDown();
     78     }
     79 }
     80