java类com.mashape.unirest.http.HttpMethod的实例源码

BucketTest.java 文件源码 项目:kinto-http-java 阅读 22 收藏 0 点赞 0 评论 0
@Test
public void listCollections() throws Exception {
    // GIVEN a fake remote kinto url
    String remote = "https://fake.kinto.url";
    // AND expected headers
    Map<String, List<String>> expectedHeaders = new HashMap<>(defaultHeaders);
    // AND a kintoClient
    KintoClient kintoClient = spy(new KintoClient(remote));
    // AND a mocked kintoClient
    doAnswer(new Answer<JSONObject>() {
        public JSONObject answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            GetRequest getRequest = (GetRequest)args[0];
            // THEN the correct endpoint is called
            assertThat(getRequest.getUrl(), is(remote + "/buckets/bucketName/collections"));
            // AND headers are corrects
            assertThat(getRequest.getHeaders(), is(expectedHeaders));
            // AND the get method is used
            assertThat(getRequest.getHttpMethod(), is(HttpMethod.GET));
            return new JSONObject("{data:[{id: \"col1\"},{id:\"col2\"}]}");
        }
    })
            .when(kintoClient)
            .execute(any(GetRequest.class));
    // WHEN calling listBuckets
    Set<Collection> collections = kintoClient.bucket("bucketName").listCollections();
    // THEN check if the answer is correctly called by checking the result

    assertThat(collections.size(), is(2));
}
BucketTest.java 文件源码 项目:kinto-http-java 阅读 23 收藏 0 点赞 0 评论 0
@Test
public void getData() throws Exception {
    // GIVEN a fake remote kinto url
    String remote = "https://fake.kinto.url";
    // AND expected headers
    Map<String, List<String>> expectedHeaders = new HashMap<>(defaultHeaders);
    // AND a kintoClient
    KintoClient kintoClient = spy(new KintoClient(remote));
    // AND a mocked kintoClient
    doAnswer(new Answer<JSONObject>() {
        public JSONObject answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            GetRequest getRequest = (GetRequest)args[0];
            // THEN the correct endpoint is called
            assertThat(getRequest.getUrl(), is(remote + "/buckets/bucketName"));
            // AND headers are corrects
            assertThat(getRequest.getHeaders(), is(expectedHeaders));
            // AND the get method is used
            assertThat(getRequest.getHttpMethod(), is(HttpMethod.GET));
            return new JSONObject("{}");
        }
    })
            .when(kintoClient)
            .execute(any(GetRequest.class));
    // WHEN calling bucket getData
    JSONObject jsonObject = kintoClient.bucket("bucketName").getData();
    // THEN check if the answer is correctly called by checking the result
    assertThat(jsonObject.toString(), is("{}"));
}
CollectionTest.java 文件源码 项目:kinto-http-java 阅读 30 收藏 0 点赞 0 评论 0
@Test
public void getData() throws Exception {
    // GIVEN a fake remote kinto url
    String remote = "https://fake.kinto.url";
    // AND expected headers
    Map<String, List<String>> expectedHeaders = new HashMap<>(defaultHeaders);
    // AND a kintoClient
    KintoClient kintoClient = spy(new KintoClient(remote));
    // AND a mocked kintoClient
    doAnswer(new Answer<JSONObject>() {
        public JSONObject answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            GetRequest getRequest = (GetRequest)args[0];
            // THEN the correct endpoint is called
            assertThat(getRequest.getUrl(), is(remote + "/buckets/bucketName/collections/collectionName"));
            // AND headers are corrects
            assertThat(getRequest.getHeaders(), is(expectedHeaders));
            // AND the get method is used
            assertThat(getRequest.getHttpMethod(), is(HttpMethod.GET));
            return new JSONObject("{}");
        }
    })
            .when(kintoClient)
            .execute(any(GetRequest.class));
    // WHEN calling bucket.collection.getData
    JSONObject jsonObject = kintoClient
            .bucket("bucketName")
            .collection("collectionName")
            .getData();
    // THEN check if the answer is correctly called by checking the result
    assertThat(jsonObject.toString(), is("{}"));
}
CollectionTest.java 文件源码 项目:kinto-http-java 阅读 34 收藏 0 点赞 0 评论 0
@Test
public void listRecords() throws Exception {
    // GIVEN a fake remote kinto url
    String remote = "https://fake.kinto.url";
    // AND expected headers
    Map<String, List<String>> expectedHeaders = new HashMap<>(defaultHeaders);
    // AND a kintoClient
    KintoClient kintoClient = spy(new KintoClient(remote));
    // AND a mocked kintoClient
    doAnswer(new Answer<JSONObject>() {
        public JSONObject answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            GetRequest getRequest = (GetRequest)args[0];
            // THEN the correct endpoint is called
            assertThat(
                    getRequest.getUrl(),
                    is(remote + "/buckets/bucketName/collections/collectionName/records")
            );
            // AND headers are corrects
            assertThat(getRequest.getHeaders(), is(expectedHeaders));
            // AND the get method is used
            assertThat(getRequest.getHttpMethod(), is(HttpMethod.GET));
            return new JSONObject("{data:[{foo: \"bar\"},{bar: \"baz\"}]}");
        }
    })
            .when(kintoClient)
            .execute(any(GetRequest.class));
    // WHEN calling bucket.collection.listRecords
    Set<JSONObject> records = kintoClient
            .bucket("bucketName")
            .collection("collectionName")
            .listRecords();
    // THEN check if the answer is correctly called by checking the result
    assertThat(records.size(), is(2));
}
CollectionTest.java 文件源码 项目:kinto-http-java 阅读 28 收藏 0 点赞 0 评论 0
@Test
public void listRecordsClazz() throws Exception {
    // GIVEN a fake remote kinto url
    String remote = "https://fake.kinto.url";
    // AND expected headers
    Map<String, List<String>> expectedHeaders = new HashMap<>(defaultHeaders);
    // AND a kintoClient
    KintoClient kintoClient = spy(new KintoClient(remote));
    // AND a mocked kintoClient
    doAnswer(new Answer<SimpleClass>() {
        public SimpleClass answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            GetRequest getRequest = (GetRequest)args[0];
            // THEN the correct endpoint is called
            assertThat(
                    getRequest.getUrl(),
                    is(remote + "/buckets/bucketName/collections/collectionName/records")
            );
            // AND headers are corrects
            assertThat(getRequest.getHeaders(), is(expectedHeaders));
            // AND the get method is used
            assertThat(getRequest.getHttpMethod(), is(HttpMethod.GET));
            return new SimpleClass("hello");
        }
    })
            .when(kintoClient)
            .execute(any(GetRequest.class), any(Class.class));
    // WHEN calling bucket.collection.listRecords
    SimpleClass object = kintoClient
            .bucket("bucketName")
            .collection("collectionName")
            .listRecords(SimpleClass.class);
    // THEN check if the answer is correctly called by checking the result
    assertThat(object.getParam(), is("hello"));
}
CollectionTest.java 文件源码 项目:kinto-http-java 阅读 30 收藏 0 点赞 0 评论 0
@Test
public void getRecord() throws Exception {
    // GIVEN a fake remote kinto url
    String remote = "https://fake.kinto.url";
    // AND expected headers
    Map<String, List<String>> expectedHeaders = new HashMap<>(defaultHeaders);
    // AND a kintoClient
    KintoClient kintoClient = spy(new KintoClient(remote));
    // AND a mocked kintoClient
    doAnswer(new Answer<JSONObject>() {
        public JSONObject answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            GetRequest getRequest = (GetRequest)args[0];
            // THEN the correct endpoint is called
            assertThat(
                    getRequest.getUrl(),
                    is(remote + "/buckets/bucketName/collections/collectionName/records/recordId")
            );
            // AND headers are corrects
            assertThat(getRequest.getHeaders(), is(expectedHeaders));
            // AND the get method is used
            assertThat(getRequest.getHttpMethod(), is(HttpMethod.GET));
            return new JSONObject("{}");
        }
    })
            .when(kintoClient)
            .execute(any(GetRequest.class));
    // WHEN calling bucket.collection.listRecords
    JSONObject jsonObject = kintoClient
            .bucket("bucketName")
            .collection("collectionName")
            .getRecord("recordId");
    // THEN check if the answer is correctly called by checking the result
    assertThat(jsonObject.toString(), is("{}"));
}
KintoClientTest.java 文件源码 项目:kinto-http-java 阅读 21 收藏 0 点赞 0 评论 0
@Test
public void testListBuckets() throws KintoException, ClientException, UnirestException {
    // GIVEN a fake remote kinto url
    String remote = "https://fake.kinto.url";
    // AND expected headers
    Map<String, List<String>> expectedHeaders = new HashMap<>(defaultHeaders);
    // AND a kintoClient
    KintoClient kintoClient = spy(new KintoClient(remote));
    // AND a mocked kintoClient
    doAnswer(new Answer<JSONObject>() {
        public JSONObject answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            GetRequest getRequest = (GetRequest)args[0];
            // THEN the correct endpoint is called
            assertThat(getRequest.getUrl(), is(remote + "/buckets"));
            // AND headers are corrects
            assertThat(getRequest.getHeaders(), is(expectedHeaders));
            // AND the get method is used
            assertThat(getRequest.getHttpMethod(), is(HttpMethod.GET));
            return new JSONObject("{}");
        }
    })
    .when(kintoClient)
    .execute(any(GetRequest.class));
    // WHEN calling listBuckets
    JSONObject jsonObject = kintoClient.listBuckets();
    // THEN check if the answer is correctly called by checking the result
    assertThat(jsonObject.toString(), is("{}"));
}
Client.java 文件源码 项目:gatekeeper 阅读 29 收藏 0 点赞 0 评论 0
public void put(String key, String value) throws Exception {
    String encrypted;

    try {
        encrypted = encryption.encrypt(value);
    } catch (Exception e) {
        throw new Exception("Could not encrypt value");
    }

    HttpRequest request = new HttpRequestWithBody(
        HttpMethod.PUT,
        makeConsulUrl(key)
    ).body(encrypted).getHttpRequest();

    authorizeHttpRequest(request);

    HttpResponse<String> response;

    try {
        response = HttpClientHelper.request(request, String.class);
    } catch (Exception exception) {
        throw new ConsulException("Consul request failed", exception);
    }

    if (!response.getBody().equals("true")) {
        throw new ConsulException(
            String.format("Consul PUT %s failed", key)
        );
    }
}
Client.java 文件源码 项目:gatekeeper 阅读 25 收藏 0 点赞 0 评论 0
public List<String> list(String key) throws Exception {
    HttpRequest request = new HttpRequestWithBody(
        HttpMethod.GET,
        makeConsulUrl(key) + "?keys&separator=/"
    ).getHttpRequest();

    authorizeHttpRequest(request);

    HttpResponse<JsonNode> response;

    try {
        response = HttpClientHelper.request(request, JsonNode.class);
    } catch (Exception exception) {
        throw new ConsulException("Consul request failed", exception);
    }

    if (response.getStatus() == 404) {
        return null;
    }

    JsonNode data = response.getBody();

    if (!data.isArray()) {
        throw new ConsulException("Malformed response - expected an array");
    }

    List<String> keys = new ArrayList<>(data.getArray().length());

    data.getArray().forEach((object) -> {
        String iter = object.toString();

        if (prefix != null) {
            iter = iter.substring(prefix.length());
        }

        keys.add(iter);
    });

    return keys;
}
AddFaceToFaceListAction.java 文件源码 项目:cognitivej 阅读 21 收藏 0 点赞 0 评论 0
private void buildContext(Object image) {
    workingContext.addQueryParameter("userData", userData)
            .setPath("face/v1.0/facelists/${faceListId}/persistedFaces").addPathVariable("faceListId", faceListId)
            .addQueryParameter("userData", userData)
            .httpMethod(HttpMethod.POST);
    if (isNotEmpty(targetFace))
        workingContext.addQueryParameter("targetFace", targetFace);
    if (image instanceof String)
        workingContext.addPayload("url", String.valueOf(image));
    if (image instanceof InputStream)
        workingContext.addPayload(IMAGE_INPUT_STREAM_KEY, image);
}


问题


面经


文章

微信
公众号

扫码关注公众号