Search in sources :

Example 1 with SignURLConfig

use of com.aliyuncs.fc.auth.SignURLConfig in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testHttpInvokeFunction.

@Test
public void testHttpInvokeFunction() throws IOException, InterruptedException, Exception {
    createService(SERVICE_NAME);
    // Create a function
    String source = generatePythonHttpCode();
    byte[] data = Util.createZipByteData("main.py", source);
    // create function
    createFunction(SERVICE_NAME, FUNCTION_NAME, "main.echo_handler", "python2.7", data);
    for (HttpAuthType auth : new HttpAuthType[] { ANONYMOUS, FUNCTION }) {
        // create http trigger
        createHttpTrigger(TRIGGER_NAME.concat(auth.toString()), auth, new HttpMethod[] { GET, POST });
        // sleep sometime so that the function cache in the API server will
        // be updated (default is 10 seconds)
        Thread.sleep(15000);
        {
            // Invoke the function
            HttpInvokeFunctionRequest request = new HttpInvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME, auth, POST, "/test/path/中文");
            request.addQuery("a", "1");
            request.addQuery("aaa", null);
            request.setHeader("Test-Header-Key", "testHeaderValue");
            request.setHeader("Content-Type", "application/json");
            request.setPayload(new String("data").getBytes());
            InvokeFunctionResponse response = client.invokeFunction(request);
            assertEquals(200, response.getStatus());
            assertTrue(response.getHeader("Content-Type").startsWith("application/json"));
            assertEquals("testHeaderValue", response.getHeader("Test-Header-Key"));
            JsonObject jsonObject = gson.fromJson(new String(response.getPayload()), JsonObject.class);
            assertEquals("/test/path/中文", jsonObject.get("path").getAsString());
            assertEquals("aaa=&a=1", jsonObject.get("queries").getAsString());
            assertEquals("data", jsonObject.get("body").getAsString());
        }
        if (auth == FUNCTION) {
            Date expires = new Date();
            expires.setTime(expires.getTime() + 5000);
            SignURLConfig input = new SignURLConfig(POST, SERVICE_NAME, FUNCTION_NAME, expires);
            input.setQualifier("LATEST");
            input.setEscapedPath("/test/path/" + AcsURLEncoder.percentEncode("中文"));
            // with header
            HashMap<String, String> header = new HashMap<String, String>();
            header.put("Test-Header-Key", "testHeaderValue");
            header.put("Content-Type", "application/json");
            input.setHeader(header);
            HashMap<String, String[]> queries = new HashMap<String, String[]>();
            queries.put("a", new String[] { "2", "3", "1" });
            queries.put("aaa", null);
            input.setQueries(queries);
            String urlLink = client.SignURL(input);
            URL url = new URL(urlLink);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestMethod("POST");
            for (String k : header.keySet()) {
                httpConn.setRequestProperty(k, header.get(k));
            }
            httpConn.setConnectTimeout(60 * 1000);
            httpConn.setReadTimeout(120 * 1000);
            httpConn.connect();
            assertEquals(200, httpConn.getResponseCode());
            Map<String, List<String>> respHeaders = httpConn.getHeaderFields();
            assertTrue(respHeaders.get("Content-Type").contains("application/json"));
            assertTrue(respHeaders.get("Test-Header-Key").contains("testHeaderValue"));
            // expires
            {
                Thread.sleep(7000);
                URL urlExpires = new URL(urlLink);
                HttpURLConnection httpConnExpires = (HttpURLConnection) urlExpires.openConnection();
                httpConnExpires.setRequestMethod("POST");
                for (String k : header.keySet()) {
                    httpConnExpires.setRequestProperty(k, header.get(k));
                }
                httpConnExpires.setConnectTimeout(60 * 1000);
                httpConnExpires.setReadTimeout(120 * 1000);
                httpConnExpires.connect();
                assertEquals(403, httpConnExpires.getResponseCode());
            }
        }
        // delete trigger
        deleteTrigger(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME.concat(auth.toString()));
    }
    // Cleanups
    client.deleteFunction(new DeleteFunctionRequest(SERVICE_NAME, FUNCTION_NAME));
    client.deleteService(new DeleteServiceRequest(SERVICE_NAME));
}
Also used : JsonObject(com.google.gson.JsonObject) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) SignURLConfig(com.aliyuncs.fc.auth.SignURLConfig) Arrays.asList(java.util.Arrays.asList)

Aggregations

SignURLConfig (com.aliyuncs.fc.auth.SignURLConfig)1 JsonObject (com.google.gson.JsonObject)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Arrays.asList (java.util.Arrays.asList)1