Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / test / ch / cyberduck / core / s3 / S3UrlProviderTest.java
blob47dca12a6286051b715b07cd6718b53e10293656
1 package ch.cyberduck.core.s3;
3 import ch.cyberduck.core.AbstractTestCase;
4 import ch.cyberduck.core.Credentials;
5 import ch.cyberduck.core.DescriptiveUrl;
6 import ch.cyberduck.core.DisabledPasswordStore;
7 import ch.cyberduck.core.Host;
8 import ch.cyberduck.core.Path;
10 import org.junit.Test;
12 import java.net.URI;
13 import java.util.EnumSet;
14 import java.util.Iterator;
16 import static org.junit.Assert.*;
18 /**
19 * @version $Id$
21 public class S3UrlProviderTest extends AbstractTestCase {
23 @Test
24 public void testToHttpURL() throws Exception {
25 final S3Session session = new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname()));
26 Path p = new Path("/bucket/f/key f", EnumSet.of(Path.Type.file));
27 assertEquals("https://bucket.s3.amazonaws.com/f/key%20f",
28 new S3UrlProvider(session).toUrl(p).find(DescriptiveUrl.Type.http).getUrl());
29 assertTrue(new S3UrlProvider(session).toUrl(p).filter(DescriptiveUrl.Type.http).contains(
30 new DescriptiveUrl(URI.create("http://bucket.s3.amazonaws.com/f/key%20f"))
31 ));
32 assertEquals(5, new S3UrlProvider(session, new DisabledPasswordStore() {
33 @Override
34 public String find(final Host host) {
35 return "k";
37 }).toUrl(p).filter(DescriptiveUrl.Type.signed).size());
40 @Test
41 public void testProviderUriWithKey() throws Exception {
42 final S3Session session = new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname()));
43 final Iterator<DescriptiveUrl> provider = new S3UrlProvider(session).toUrl(new Path("/test.cyberduck.ch/key",
44 EnumSet.of(Path.Type.file))).filter(DescriptiveUrl.Type.provider).iterator();
45 assertEquals("https://s3.amazonaws.com/test.cyberduck.ch/key", provider.next().getUrl());
46 assertEquals("s3://test.cyberduck.ch/key", provider.next().getUrl());
49 @Test
50 public void testProviderUriRoot() throws Exception {
51 final S3Session session = new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname()));
52 final Iterator<DescriptiveUrl> provider = new S3UrlProvider(session).toUrl(new Path("/test.cyberduck.ch",
53 EnumSet.of(Path.Type.directory))).filter(DescriptiveUrl.Type.provider).iterator();
54 assertEquals("https://s3.amazonaws.com/test.cyberduck.ch", provider.next().getUrl());
55 assertEquals("s3://test.cyberduck.ch/", provider.next().getUrl());
58 @Test
59 public void testHttpUri() throws Exception {
60 final S3Session session = new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname()));
61 assertEquals("https://test.cyberduck.ch.s3.amazonaws.com/key",
62 new S3UrlProvider(session).toUrl(new Path("/test.cyberduck.ch/key", EnumSet.of(Path.Type.file))).find(DescriptiveUrl.Type.http).getUrl());
65 @Test
66 public void testToSignedUrlAnonymous() throws Exception {
67 final S3Session session = new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(),
68 new Credentials("anonymous", null)));
69 assertEquals(DescriptiveUrl.EMPTY,
70 new S3UrlProvider(session, new DisabledPasswordStore() {
71 @Override
72 public String find(final Host host) {
73 return "k";
75 }).toUrl(new Path("/test.cyberduck.ch/test f", EnumSet.of(Path.Type.file))).find(DescriptiveUrl.Type.signed)
79 @Test
80 public void testToSignedUrlThirdparty() throws Exception {
81 final S3Session session = new S3Session(new Host(new S3Protocol(), "s.greenqloud.com",
82 new Credentials("k", "s")));
83 final S3UrlProvider provider = new S3UrlProvider(session, new DisabledPasswordStore() {
84 @Override
85 public String find(final Host host) {
86 return "k";
88 });
89 assertNotNull(
90 provider.toUrl(new Path("/test.cyberduck.ch/test", EnumSet.of(Path.Type.file))).find(DescriptiveUrl.Type.signed)
94 @Test
95 public void testToSignedUrl() throws Exception {
96 final S3Session session = new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(), new Credentials(
97 properties.getProperty("s3.key"), null
98 )));
99 final S3UrlProvider provider = new S3UrlProvider(session, new DisabledPasswordStore() {
100 @Override
101 public String find(final Host host) {
102 return "k";
105 assertTrue(provider.sign(new Path("/test.cyberduck.ch/test", EnumSet.of(Path.Type.file)), 30).getUrl().startsWith(
106 "https://test.cyberduck.ch.s3.amazonaws.com/test?AWSAccessKeyId=AKIAIGNLFZ2PXC6H2UPQ&Expires="));
109 @Test
110 public void testToTorrentUrl() throws Exception {
111 final S3Session session = new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(),
112 new Credentials("anonymous", null)));
113 assertEquals(new DescriptiveUrl(URI.create("http://test.cyberduck.ch.s3.amazonaws.com/test%20f?torrent"), DescriptiveUrl.Type.torrent),
114 new S3UrlProvider(session).toUrl(new Path("/test.cyberduck.ch/test f", EnumSet.of(Path.Type.file))).find(DescriptiveUrl.Type.torrent));
117 @Test
118 public void testToTorrentUrlThirdparty() throws Exception {
119 final S3Session session = new S3Session(new Host(new S3Protocol(), "test.cyberduck.ch",
120 new Credentials("anonymous", null)));
121 assertEquals(new DescriptiveUrl(URI.create("http://test.cyberduck.ch/c/test%20f?torrent"), DescriptiveUrl.Type.torrent),
122 new S3UrlProvider(session).toUrl(new Path("/c/test f", EnumSet.of(Path.Type.file))).find(DescriptiveUrl.Type.torrent));
125 @Test
126 public void testPlaceholder() throws Exception {
127 final S3Session session = new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(), new Credentials(
128 properties.getProperty("s3.key"), null
129 )));
130 assertTrue(
131 new S3UrlProvider(session).toUrl(new Path("/test.cyberduck.ch/test", EnumSet.of(Path.Type.directory))).filter(DescriptiveUrl.Type.signed).isEmpty());