Merge branch 'main' into add-filter-tests

This commit is contained in:
kegsay 2022-09-26 10:29:26 +01:00 committed by GitHub
commit dcac0ce8bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 9 deletions

4
.gitignore vendored
View File

@ -2,6 +2,7 @@
*.o *.o
*.a *.a
*.so *.so
*.out
# Folders # Folders
_obj _obj
@ -22,3 +23,6 @@ _testmain.go
*.exe *.exe
*.test *.test
*.prof *.prof
# test editor files
*.swp

1
CHANGELOG.md Normal file
View File

@ -0,0 +1 @@
## Release 0.1.0 (UNRELEASED)

View File

@ -4,3 +4,68 @@
A Golang Matrix client. A Golang Matrix client.
**THIS IS UNDER ACTIVE DEVELOPMENT: BREAKING CHANGES ARE FREQUENT.** **THIS IS UNDER ACTIVE DEVELOPMENT: BREAKING CHANGES ARE FREQUENT.**
# Contributing
All contributions are greatly appreciated!
## How to report issues
Please check the current open issues for similar reports
in order to avoid duplicates.
Some general guidelines:
- Include a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) when possible.
- Describe the expected behaviour and what actually happened
including a full trace-back in case of exceptions.
- Make sure to list details about your environment
## Setting up your environment
If you intend to contribute to gomatrix you'll first need Go installed on your machine (version 1.12+ is required). Also, make sure to have golangci-lint properly set up since we use it for pre-commit hooks (for instructions on how to install it, check the [official docs](https://golangci-lint.run/usage/install/#local-installation)).
- Fork gomatrix to your GitHub account by clicking the [Fork](https://github.com/matrix-org/gomatrix/fork) button.
- [Clone](https://help.github.com/en/articles/fork-a-repo#step-2-create-a-local-clone-of-your-fork) the main repository (not your fork) to your local machine.
$ git clone https://github.com/matrix-org/gomatrix
$ cd gomatrix
- Add your fork as a remote to push your contributions.Replace
``{username}`` with your username.
git remote add fork https://github.com/{username}/gomatrix
- Create a new branch to identify what feature you are working on.
$ git fetch origin
$ git checkout -b your-branch-name origin/master
- Make your changes, including tests that cover any code changes you make, and run them as described below.
- Execute pre-commit hooks by running
<gomatrix dir>/hooks/pre-commit
- Push your changes to your fork and [create a pull request](https://help.github.com/en/articles/creating-a-pull-request) describing your changes.
$ git push --set-upstream fork your-branch-name
- Finally, create a [pull request](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests)
## How to run tests
You can run the test suite and example code with `$ go test -v`
# Running Coverage
To run coverage, first generate the coverage report using `go test`
go test -v -cover -coverprofile=coverage.out
You can now show the generated report as a html page with `go tool`
go tool cover -html=coverage.out

View File

@ -53,13 +53,13 @@ func (e HTTPError) Error() string {
return fmt.Sprintf("contents=%v msg=%s code=%d wrapped=%s", e.Contents, e.Message, e.Code, wrappedErrMsg) return fmt.Sprintf("contents=%v msg=%s code=%d wrapped=%s", e.Contents, e.Message, e.Code, wrappedErrMsg)
} }
// BuildURL builds a URL with the Client's homserver/prefix/access_token set already. // BuildURL builds a URL with the Client's homeserver/prefix set already.
func (cli *Client) BuildURL(urlPath ...string) string { func (cli *Client) BuildURL(urlPath ...string) string {
ps := append([]string{cli.Prefix}, urlPath...) ps := append([]string{cli.Prefix}, urlPath...)
return cli.BuildBaseURL(ps...) return cli.BuildBaseURL(ps...)
} }
// BuildBaseURL builds a URL with the Client's homeserver/access_token set already. You must // BuildBaseURL builds a URL with the Client's homeserver set already. You must
// supply the prefix in the path. // supply the prefix in the path.
func (cli *Client) BuildBaseURL(urlPath ...string) string { func (cli *Client) BuildBaseURL(urlPath ...string) string {
// copy the URL. Purposefully ignore error as the input is from a valid URL already // copy the URL. Purposefully ignore error as the input is from a valid URL already
@ -72,9 +72,6 @@ func (cli *Client) BuildBaseURL(urlPath ...string) string {
hsURL.Path = hsURL.Path + "/" hsURL.Path = hsURL.Path + "/"
} }
query := hsURL.Query() query := hsURL.Query()
if cli.AccessToken != "" {
query.Set("access_token", cli.AccessToken)
}
if cli.AppServiceUserID != "" { if cli.AppServiceUserID != "" {
query.Set("user_id", cli.AppServiceUserID) query.Set("user_id", cli.AppServiceUserID)
} }
@ -82,7 +79,7 @@ func (cli *Client) BuildBaseURL(urlPath ...string) string {
return hsURL.String() return hsURL.String()
} }
// BuildURLWithQuery builds a URL with query parameters in addition to the Client's homeserver/prefix/access_token set already. // BuildURLWithQuery builds a URL with query parameters in addition to the Client's homeserver/prefix set already.
func (cli *Client) BuildURLWithQuery(urlPath []string, urlQuery map[string]string) string { func (cli *Client) BuildURLWithQuery(urlPath []string, urlQuery map[string]string) string {
u, _ := url.Parse(cli.BuildURL(urlPath...)) u, _ := url.Parse(cli.BuildURL(urlPath...))
q := u.Query() q := u.Query()
@ -203,7 +200,13 @@ func (cli *Client) MakeRequest(method string, httpURL string, reqBody interface{
if err != nil { if err != nil {
return err return err
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
if cli.AccessToken != "" {
req.Header.Set("Authorization", "Bearer "+cli.AccessToken)
}
res, err := cli.Client.Do(req) res, err := cli.Client.Do(req)
if res != nil { if res != nil {
defer res.Body.Close() defer res.Body.Close()
@ -687,15 +690,21 @@ func (cli *Client) UploadToContentRepo(content io.Reader, contentType string, co
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Set("Content-Type", contentType) req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", "Bearer "+cli.AccessToken)
req.ContentLength = contentLength req.ContentLength = contentLength
res, err := cli.Client.Do(req) res, err := cli.Client.Do(req)
if res != nil { if res != nil {
defer res.Body.Close() defer res.Body.Close()
} }
if err != nil { if err != nil {
return nil, err return nil, err
} }
if res.StatusCode != 200 { if res.StatusCode != 200 {
contents, err := ioutil.ReadAll(res.Body) contents, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
@ -710,10 +719,12 @@ func (cli *Client) UploadToContentRepo(content io.Reader, contentType string, co
Code: res.StatusCode, Code: res.StatusCode,
} }
} }
var m RespMediaUpload var m RespMediaUpload
if err := json.NewDecoder(res.Body).Decode(&m); err != nil { if err := json.NewDecoder(res.Body).Decode(&m); err != nil {
return nil, err return nil, err
} }
return &m, nil return &m, nil
} }

View File

@ -55,7 +55,7 @@ func ExampleClient_BuildURLWithQuery() {
"filter_id": "5", "filter_id": "5",
}) })
fmt.Println(out) fmt.Println(out)
// Output: https://matrix.org/_matrix/client/r0/sync?access_token=abcdef123456&filter_id=5 // Output: https://matrix.org/_matrix/client/r0/sync?filter_id=5
} }
func ExampleClient_BuildURL() { func ExampleClient_BuildURL() {
@ -63,7 +63,7 @@ func ExampleClient_BuildURL() {
cli, _ := NewClient("https://matrix.org", userID, "abcdef123456") cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
out := cli.BuildURL("user", userID, "filter") out := cli.BuildURL("user", userID, "filter")
fmt.Println(out) fmt.Println(out)
// Output: https://matrix.org/_matrix/client/r0/user/@example:matrix.org/filter?access_token=abcdef123456 // Output: https://matrix.org/_matrix/client/r0/user/@example:matrix.org/filter
} }
func ExampleClient_BuildBaseURL() { func ExampleClient_BuildBaseURL() {
@ -71,7 +71,7 @@ func ExampleClient_BuildBaseURL() {
cli, _ := NewClient("https://matrix.org", userID, "abcdef123456") cli, _ := NewClient("https://matrix.org", userID, "abcdef123456")
out := cli.BuildBaseURL("_matrix", "client", "r0", "directory", "room", "#matrix:matrix.org") out := cli.BuildBaseURL("_matrix", "client", "r0", "directory", "room", "#matrix:matrix.org")
fmt.Println(out) fmt.Println(out)
// Output: https://matrix.org/_matrix/client/r0/directory/room/%23matrix:matrix.org?access_token=abcdef123456 // Output: https://matrix.org/_matrix/client/r0/directory/room/%23matrix:matrix.org
} }
// Retrieve the content of a m.room.name state event. // Retrieve the content of a m.room.name state event.

View File

@ -150,5 +150,8 @@ type MockRoundTripper struct {
} }
func (t MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { func (t MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if req.Header.Get("Authorization") == "" {
panic("no auth")
}
return t.RT(req) return t.RT(req)
} }