/*
 * Copyright 2002-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.util;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Unit tests for {@link DefaultUriTemplateHandler}.
 *
 * @author Rossen Stoyanchev
 */
@SuppressWarnings("deprecation")
public class DefaultUriTemplateHandlerTests {

	private final DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler();


	@Test
	public void baseUrlWithoutPath() throws Exception {
		this.handler.setBaseUrl("http://localhost:8080");
		URI actual = this.handler.expand("/myapiresource");

		assertThat(actual.toString()).isEqualTo("http://localhost:8080/myapiresource");
	}

	@Test
	public void baseUrlWithPath() throws Exception {
		this.handler.setBaseUrl("http://localhost:8080/context");
		URI actual = this.handler.expand("/myapiresource");

		assertThat(actual.toString()).isEqualTo("http://localhost:8080/context/myapiresource");
	}

	@Test	// SPR-14147
	public void defaultUriVariables() throws Exception {
		Map<String, String> defaultVars = new HashMap<>(2);
		defaultVars.put("host", "api.example.com");
		defaultVars.put("port", "443");
		this.handler.setDefaultUriVariables(defaultVars);

		Map<String, Object> vars = new HashMap<>(1);
		vars.put("id", 123L);

		String template = "https://{host}:{port}/v42/customers/{id}";
		URI actual = this.handler.expand(template, vars);

		assertThat(actual.toString()).isEqualTo("https://api.example.com:443/v42/customers/123");
	}

	@Test
	public void parsePathIsOff() throws Exception {
		this.handler.setParsePath(false);
		Map<String, String> vars = new HashMap<>(2);
		vars.put("hotel", "1");
		vars.put("publicpath", "pics/logo.png");
		String template = "https://example.com/hotels/{hotel}/pic/{publicpath}";
		URI actual = this.handler.expand(template, vars);

		assertThat(actual.toString()).isEqualTo("https://example.com/hotels/1/pic/pics/logo.png");
	}

	@Test
	public void parsePathIsOn() throws Exception {
		this.handler.setParsePath(true);
		Map<String, String> vars = new HashMap<>(2);
		vars.put("hotel", "1");
		vars.put("publicpath", "pics/logo.png");
		vars.put("scale", "150x150");
		String template = "https://example.com/hotels/{hotel}/pic/{publicpath}/size/{scale}";
		URI actual = this.handler.expand(template, vars);

		assertThat(actual.toString()).isEqualTo("https://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150");
	}

	@Test
	public void strictEncodingIsOffWithMap() throws Exception {
		this.handler.setStrictEncoding(false);
		Map<String, String> vars = new HashMap<>(2);
		vars.put("userId", "john;doe");
		String template = "https://www.example.com/user/{userId}/dashboard";
		URI actual = this.handler.expand(template, vars);

		assertThat(actual.toString()).isEqualTo("https://www.example.com/user/john;doe/dashboard");
	}

	@Test
	public void strictEncodingOffWithArray() throws Exception {
		this.handler.setStrictEncoding(false);
		String template = "https://www.example.com/user/{userId}/dashboard";
		URI actual = this.handler.expand(template, "john;doe");

		assertThat(actual.toString()).isEqualTo("https://www.example.com/user/john;doe/dashboard");
	}

	@Test
	public void strictEncodingOnWithMap() throws Exception {
		this.handler.setStrictEncoding(true);
		Map<String, String> vars = new HashMap<>(2);
		vars.put("userId", "john;doe");
		String template = "https://www.example.com/user/{userId}/dashboard";
		URI actual = this.handler.expand(template, vars);

		assertThat(actual.toString()).isEqualTo("https://www.example.com/user/john%3Bdoe/dashboard");
	}

	@Test
	public void strictEncodingOnWithArray() throws Exception {
		this.handler.setStrictEncoding(true);
		String template = "https://www.example.com/user/{userId}/dashboard";
		URI actual = this.handler.expand(template, "john;doe");

		assertThat(actual.toString()).isEqualTo("https://www.example.com/user/john%3Bdoe/dashboard");
	}

	@Test	// SPR-14147
	public void strictEncodingAndDefaultUriVariables() throws Exception {
		Map<String, String> defaultVars = new HashMap<>(1);
		defaultVars.put("host", "www.example.com");
		this.handler.setDefaultUriVariables(defaultVars);
		this.handler.setStrictEncoding(true);

		Map<String, Object> vars = new HashMap<>(1);
		vars.put("userId", "john;doe");

		String template = "https://{host}/user/{userId}/dashboard";
		URI actual = this.handler.expand(template, vars);

		assertThat(actual.toString()).isEqualTo("https://www.example.com/user/john%3Bdoe/dashboard");
	}

}
