Skip to content

Commit 3c9fac1

Browse files
committed
Fix the NPM package listing
1 parent c025ebf commit 3c9fac1

2 files changed

Lines changed: 112 additions & 2 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Copyright (C) 2011-2020 Red Hat, Inc. (https://github.com/Commonjava/indy)
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+
package org.commonjava.indy.content.browse.ftest;
17+
18+
import org.apache.commons.io.IOUtils;
19+
import org.commonjava.indy.ftest.core.AbstractContentManagementTest;
20+
import org.commonjava.indy.model.core.HostedRepository;
21+
import org.commonjava.indy.model.core.StoreKey;
22+
import org.commonjava.indy.pkg.maven.model.MavenPackageTypeDescriptor;
23+
import org.commonjava.indy.pkg.npm.model.NPMPackageTypeDescriptor;
24+
import org.junit.Test;
25+
26+
import java.io.ByteArrayInputStream;
27+
import java.io.InputStream;
28+
import java.net.URL;
29+
30+
import static org.commonjava.indy.model.core.StoreType.hosted;
31+
import static org.hamcrest.CoreMatchers.equalTo;
32+
import static org.hamcrest.CoreMatchers.notNullValue;
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertThat;
35+
36+
/**
37+
* Verifies that the available packages are listed via browse api.
38+
* <br/>
39+
* Given:
40+
* <ul>
41+
* <li>Content is stored in a NPM hosted repository</li>
42+
* </ul>
43+
* <br/>
44+
* When:
45+
* <ul>
46+
* <li>A directory on the stored content's path is requested via GET.
47+
* <br/>
48+
* <ul>
49+
* <li>directly from the hosted repository or from the scoped level</li>
50+
* <li>without a trailing '/' or a '/index.html' in the requested path</li>
51+
* <li>without using Accept: application/json</li>
52+
* </ul>
53+
* </li>
54+
* </ul>
55+
* <br/>
56+
* Then:
57+
* <ul>
58+
* <li>The directory listing should be rendered to HTML</li>
59+
* </ul>
60+
*/
61+
public class StoreNPMThenListingDirTest
62+
extends AbstractContentManagementTest
63+
{
64+
65+
@Test
66+
public void run()
67+
throws Exception
68+
{
69+
70+
final String changelog = "Create test structures";
71+
72+
final HostedRepository hostedRepository =
73+
this.client.stores()
74+
.create( new HostedRepository(NPMPackageTypeDescriptor.NPM_PKG_KEY, STORE ), changelog, HostedRepository.class );
75+
76+
final String content = "This is a test: " + System.nanoTime();
77+
final InputStream stream = new ByteArrayInputStream( content.getBytes() );
78+
79+
final String dirPath = "/@babel/opossum";
80+
final String path = dirPath + "/package.json";
81+
82+
final StoreKey testKey = hostedRepository.getKey();
83+
84+
assertThat( client.content().exists( testKey, path ), equalTo( false ) );
85+
86+
client.content().store( testKey, path, stream );
87+
88+
assertThat( client.content().exists( testKey, path ), equalTo( true ) );
89+
90+
try(InputStream jsonIn = client.content().get( testKey, "@babel" ))
91+
{
92+
assertThat( jsonIn, notNullValue() );
93+
String json = IOUtils.toString( jsonIn );
94+
assertThat( json.startsWith( "{" ), equalTo( true ) );
95+
assertThat( json.contains( "/@babel/opossum/" ), equalTo( true ) );
96+
}
97+
98+
try(InputStream jsonIn = client.content().get( testKey, "/" ))
99+
{
100+
assertThat( jsonIn, notNullValue() );
101+
String json = IOUtils.toString( jsonIn );
102+
assertThat( json.startsWith( "{" ), equalTo( true ) );
103+
assertThat( json.contains( "/@babel/" ), equalTo( true ) );
104+
}
105+
106+
assertThat( client.content()
107+
.exists( testKey, path ), equalTo( true ) );
108+
109+
}
110+
}

addons/pkg-npm/common/src/main/java/org/commonjava/indy/pkg/npm/content/NPMStoragePathCalculator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ public String calculateStoragePath( final StoreKey key, final String path )
8686
}
8787

8888
// This is considering the single path for npm standard like "/jquery"
89-
final boolean isSinglePath = pkg.split( "/" ).length < 2;
89+
final boolean isSinglePath = !pkg.startsWith( "@" ) && pkg.split( "/" ).length == 1;
9090
// This is considering the scoped path for npm standard like "/@type/jquery"
91-
final boolean isScopedPath = pkg.startsWith( "@" ) && pkg.split( "/" ).length < 3;
91+
final boolean isScopedPath = pkg.startsWith( "@" ) && pkg.split( "/" ).length == 2;
9292
if ( isSinglePath || isScopedPath )
9393
{
9494
logger.debug( "Modifying target path: {}, appending '{}', store {}", path, NPM_METADATA_NAME,

0 commit comments

Comments
 (0)