File: url_test.go

package info (click to toggle)
golang-maunium-go-mautrix 0.11.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,184 kB
  • sloc: sh: 6; makefile: 5
file content (65 lines) | stat: -rw-r--r-- 2,732 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2022 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package mautrix_test

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"maunium.net/go/mautrix"
)

func TestClient_BuildURL(t *testing.T) {
	cli, err := mautrix.NewClient("https://example.com", "", "")
	assert.NoError(t, err)
	assert.Equal(t, cli.HomeserverURL.Scheme, "https")
	assert.Equal(t, cli.HomeserverURL.Host, "example.com")
	assert.Equal(t, cli.HomeserverURL.Path, "")
	built := cli.BuildClientURL("v3", "foo/bar%2F🐈 1", "hello", "world")
	assert.Equal(t, "https://example.com/_matrix/client/v3/foo%2Fbar%252F%F0%9F%90%88%201/hello/world", built)
}

func TestClient_BuildURL_HTTP(t *testing.T) {
	cli, err := mautrix.NewClient("http://example.com", "", "")
	assert.NoError(t, err)
	assert.Equal(t, cli.HomeserverURL.Scheme, "http")
	assert.Equal(t, cli.HomeserverURL.Host, "example.com")
	assert.Equal(t, cli.HomeserverURL.Path, "")
	built := cli.BuildClientURL("v3", "foo/bar%2F🐈 1", "hello", "world")
	assert.Equal(t, "http://example.com/_matrix/client/v3/foo%2Fbar%252F%F0%9F%90%88%201/hello/world", built)
}

func TestClient_BuildURL_MissingScheme(t *testing.T) {
	cli, err := mautrix.NewClient("example.com", "", "")
	assert.NoError(t, err)
	assert.Equal(t, cli.HomeserverURL.Scheme, "https")
	assert.Equal(t, cli.HomeserverURL.Host, "example.com")
	assert.Equal(t, cli.HomeserverURL.Path, "")
	built := cli.BuildClientURL("v3", "foo/bar%2F🐈 1", "hello", "world")
	assert.Equal(t, "https://example.com/_matrix/client/v3/foo%2Fbar%252F%F0%9F%90%88%201/hello/world", built)
}

func TestClient_BuildURL_WithPath(t *testing.T) {
	cli, err := mautrix.NewClient("https://example.com/base", "", "")
	assert.NoError(t, err)
	assert.Equal(t, cli.HomeserverURL.Scheme, "https")
	assert.Equal(t, cli.HomeserverURL.Host, "example.com")
	assert.Equal(t, cli.HomeserverURL.Path, "/base")
	built := cli.BuildClientURL("v3", "foo/bar%2F🐈 1", "hello", "world")
	assert.Equal(t, "https://example.com/base/_matrix/client/v3/foo%2Fbar%252F%F0%9F%90%88%201/hello/world", built)
}

func TestClient_BuildURL_MissingSchemeWithPath(t *testing.T) {
	cli, err := mautrix.NewClient("example.com/base", "", "")
	assert.NoError(t, err)
	assert.Equal(t, cli.HomeserverURL.Scheme, "https")
	assert.Equal(t, cli.HomeserverURL.Host, "example.com")
	assert.Equal(t, cli.HomeserverURL.Path, "/base")
	built := cli.BuildClientURL("v3", "foo/bar%2F🐈 1", "hello", "world")
	assert.Equal(t, "https://example.com/base/_matrix/client/v3/foo%2Fbar%252F%F0%9F%90%88%201/hello/world", built)
}