public String artifactLocator(String filePath) { //TODO: we should make sure data is valid at the beginning instead of fixing it here if (filePath.startsWith("/")) { filePath = filePath.substring(1); } return UrlUtil.encodeInUtf8(String.format("%s/%s/%s", stageLocator(), buildName, filePath)); }
private String getPropertiesUrl(String propertyName) { return UrlUtil.concatPath(propertyBaseUrl, UrlUtil.encodeInUtf8(propertyName)); }
@Test public void concatPathWithBaseUrl() throws Exception { assertThat(UrlUtil.concatPath("http://foo", "bar"), is("http://foo/bar")); assertThat(UrlUtil.concatPath("http://foo/", "bar"), is("http://foo/bar")); } }
@Test public void shouldGetGivenQueryParamFromUrl() throws Exception { String url = "http://localhost:8153?code=123&new_code=xyz"; assertThat(UrlUtil.getQueryParamFromUrl(url, "code"),is("123")); assertThat(UrlUtil.getQueryParamFromUrl(url, "new_code"),is("xyz")); }
@Test public void shouldAppendQueryString() throws Exception { assertThat(UrlUtil.urlWithQuery("http://baz.quux", "foo", "bar"), is("http://baz.quux?foo=bar")); assertThat(UrlUtil.urlWithQuery("http://baz.quux?bang=boom&hello=world", "foo", "bar"), is("http://baz.quux?bang=boom&hello=world&foo=bar")); assertThat(UrlUtil.urlWithQuery("http://baz.quux:1000/hello/world?bang=boom", "foo", "bar"), is("http://baz.quux:1000/hello/world?bang=boom&foo=bar")); assertThat(UrlUtil.urlWithQuery("http://baz.quux:1000/hello/world?bang=boom%20bang&quux=bar/baz&sha1=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D", "foo", "bar\\baz"), is("http://baz.quux:1000/hello/world?bang=boom+bang&quux=bar%2Fbaz&sha1=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D&foo=bar%5Cbaz")); assertThat(UrlUtil.urlWithQuery("http://baz.quux:1000/hello/world?bang=boom#in_hell", "foo", "bar"), is("http://baz.quux:1000/hello/world?bang=boom&foo=bar#in_hell")); assertThat(UrlUtil.urlWithQuery("http://user:loser@baz.quux:1000/hello/world#in_hell", "foo", "bar"), is("http://user:loser@baz.quux:1000/hello/world?foo=bar#in_hell")); }
@Test public void shouldReturnEmptyStringIfQueryParamIsNotAvailable() throws Exception { String url = "http://localhost:8153?code=123&new_code=xyz"; assertThat(UrlUtil.getQueryParamFromUrl(url, "not_available"),is("")); }
public String buildLocator() { //TODO: the encoding logic should be moved to presentation layer return UrlUtil.encodeInUtf8(String.format("%s/%s", stageLocator(), buildName)); }
private String getUploadUrl(String buildId, String normalizedDestPath, int publishingAttempts) { String path = format("%s?attempt=%d&buildId=%s", UrlUtil.encodeInUtf8(normalizedDestPath), publishingAttempts, buildId); return UrlUtil.concatPath(artifactsBaseUrl, path); }
@Test public void shouldReturnEmptyStringIfUrlIsInvalid() throws Exception { String url = "this is not valid url"; assertThat(UrlUtil.getQueryParamFromUrl(url, "param"),is("")); }
public String propertyLocator(String propertyName) { return UrlUtil.encodeInUtf8(String.format("%s/%s/%s", stageLocator(), buildName, propertyName)); }
private List stageHistoryAsJson(PipelineInstanceModel pipelineInstanceModel, StageInstanceModels stageHistory) { List json = new ArrayList(); for (StageInstanceModel stageHistoryItem : stageHistory) { Map<String, Object> jsonMap = new LinkedHashMap<>(); jsonMap.put("stageName", stageHistoryItem.getName()); jsonMap.put("stageId", stageHistoryItem.getId()); jsonMap.put("stageStatus", stageHistoryItem.getState().toString()); StageIdentifier stageIdentifier = new StageIdentifier(pipelineInstanceModel.getPipelineIdentifier(), stageHistoryItem.getName(), stageHistoryItem.getCounter()); jsonMap.put("stageLocator", encodeInUtf8(stageIdentifier.stageLocator())); jsonMap.put("getCanRun", Boolean.toString(stageHistoryItem.getCanRun())); jsonMap.put("getCanCancel", Boolean.toString(stageHistoryItem.getCanCancel())); jsonMap.put("scheduled", Boolean.toString(stageHistoryItem.isScheduled())); jsonMap.put("stageCounter", stageHistoryItem.getCounter()); handleApproval(stageHistoryItem, jsonMap); json.add(jsonMap); } return json; }
@Test public void shouldEncodeAllPartsInUrl() { assertThat(UrlUtil.encodeInUtf8("a%b/c%d"), is("a%25b/c%25d")); }
@Test public void shouldKeepTrailingSlash() { assertThat(UrlUtil.encodeInUtf8("a%b/c%d/"), is("a%25b/c%25d/")); }
@Test public void shouldKeepPrecedingSlash() { assertThat(UrlUtil.encodeInUtf8("/a%b/c%d"), is("/a%25b/c%25d")); }
@Test public void shouldEncodeUrl() { assertThat(UrlUtil.encodeInUtf8("a%b"), is("a%25b")); }