|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * wcm.io |
| 4 | + * %% |
| 5 | + * Copyright (C) 2016 wcm.io |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package io.wcm.devops.maven.nodejsproxy.resource; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStream; |
| 27 | + |
| 28 | +import javax.ws.rs.core.MediaType; |
| 29 | +import javax.ws.rs.core.Response; |
| 30 | + |
| 31 | +import org.apache.commons.codec.digest.DigestUtils; |
| 32 | +import org.apache.commons.io.IOUtils; |
| 33 | +import org.apache.http.HttpStatus; |
| 34 | +import org.junit.Rule; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +import io.dropwizard.testing.junit.ResourceTestRule; |
| 38 | + |
| 39 | + |
| 40 | +public class MavenProxyResourceTest { |
| 41 | + |
| 42 | + // test with the following NodeJS and NPM versions |
| 43 | + private static final String[] NODEJS_VERSIONS = { |
| 44 | + "0.12.0" |
| 45 | + }; |
| 46 | + private static final String[] NODEJS_TARGETS = { |
| 47 | + "-windows-x86.exe", |
| 48 | + "-windows-x64.exe", |
| 49 | + "-linux-x86.tar.gz", |
| 50 | + "-linux-x64.tar.gz", |
| 51 | + "-darwin-x86.tar.gz", |
| 52 | + "-darwin-x64.tar.gz", |
| 53 | + }; |
| 54 | + private static final String[] NPM_VERSIONS = { |
| 55 | + "1.4.9" |
| 56 | + }; |
| 57 | + private static final String[] NPM_TARGETS = { |
| 58 | + ".tgz", |
| 59 | + }; |
| 60 | + |
| 61 | + @Rule |
| 62 | + public ResourceTestRule context = new ResourceTestRule.Builder() |
| 63 | + .addResource(new MavenProxyResource(TestContext.getConfiguration(), TestContext.getHttpClient())) |
| 64 | + .build(); |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testGetIndex() { |
| 68 | + String path = "/"; |
| 69 | + Response response = context.client().target(path).request().get(); |
| 70 | + assertResponse(path, response, MediaType.TEXT_HTML); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testGetPomNodeJS() { |
| 75 | + for (String version : NODEJS_VERSIONS) { |
| 76 | + String path = "/org/nodejs/dist/nodejs-binaries/" + version + "/nodejs-binaries-" + version + ".pom"; |
| 77 | + Response response = context.client().target(path).request().get(); |
| 78 | + assertResponse(path, response, MediaType.APPLICATION_XML); |
| 79 | + assertTrue("Content length " + path, response.getLength() > 0); |
| 80 | + assertSHA1(path, response); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testGetPomNPM() { |
| 86 | + for (String version : NPM_VERSIONS) { |
| 87 | + String path = "/org/nodejs/dist/npm-binaries/" + version + "/npm-binaries-" + version + ".pom"; |
| 88 | + Response response = context.client().target(path).request().get(); |
| 89 | + assertResponse(path, response, MediaType.APPLICATION_XML); |
| 90 | + assertTrue("Content length " + path, response.getLength() > 0); |
| 91 | + assertSHA1(path, response); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testGetBinaryNodeJS() { |
| 97 | + for (String version : NODEJS_VERSIONS) { |
| 98 | + for (String target : NODEJS_TARGETS) { |
| 99 | + String path = "/org/nodejs/dist/nodejs-binaries/" + version + "/nodejs-binaries-" + version + target; |
| 100 | + Response response = context.client().target(path).request().get(); |
| 101 | + assertResponse(path, response, MediaType.APPLICATION_OCTET_STREAM); |
| 102 | + assertSHA1(path, response); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testGetBinaryNPM() { |
| 109 | + for (String version : NPM_VERSIONS) { |
| 110 | + for (String target : NPM_TARGETS) { |
| 111 | + String path = "/org/nodejs/dist/npm-binaries/" + version + "/npm-binaries-" + version + target; |
| 112 | + Response response = context.client().target(path).request().get(); |
| 113 | + assertResponse(path, response, MediaType.APPLICATION_OCTET_STREAM); |
| 114 | + assertSHA1(path, response); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void assertResponse(String path, Response response, String mediaType) { |
| 120 | + assertEquals("HTTP status " + path, HttpStatus.SC_OK, response.getStatus()); |
| 121 | + assertEquals("Media type " + path, mediaType, response.getMediaType().toString()); |
| 122 | + assertTrue(response.hasEntity()); |
| 123 | + } |
| 124 | + |
| 125 | + private void assertSHA1(String path, Response dataResponse) { |
| 126 | + String sha1Path = path + ".sha1"; |
| 127 | + Response sha1Response = context.client().target(sha1Path).request().get(); |
| 128 | + assertResponse(sha1Path, sha1Response, MediaType.TEXT_PLAIN); |
| 129 | + |
| 130 | + try (InputStream is = dataResponse.readEntity(InputStream.class)) { |
| 131 | + byte[] data = IOUtils.toByteArray(is); |
| 132 | + String sha1 = sha1Response.readEntity(String.class); |
| 133 | + assertEquals(sha1, DigestUtils.sha1Hex(data)); |
| 134 | + } |
| 135 | + catch (IOException ex) { |
| 136 | + throw new RuntimeException("Error checking SHA-1 of " + path, ex); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments