Skip to content

Commit

Permalink
A few simple test case to get us started and run them in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
hughns committed Jan 20, 2025
1 parent 673d744 commit cc43c8e
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 4 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test

on:
pull_request:
push:
branches: [main]

jobs:
test:
name: Testing
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Test
run: go test -timeout 30s
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ func (h *Handler) handle(w http.ResponseWriter, r *http.Request) {
}
}

func (h *Handler) prepareMux() (*http.ServeMux) {

mux := http.NewServeMux()
mux.HandleFunc("/sfu/get", h.handle)
mux.HandleFunc("/healthz", h.healthcheck)

return mux
}

func main() {
skipVerifyTLS := os.Getenv("LIVEKIT_INSECURE_SKIP_VERIFY_TLS") == "YES_I_KNOW_WHAT_I_AM_DOING"
if skipVerifyTLS {
Expand Down Expand Up @@ -203,10 +212,7 @@ func main() {
skipVerifyTLS: skipVerifyTLS,
}

http.HandleFunc("/sfu/get", handler.handle)
http.HandleFunc("/healthz", handler.healthcheck)

log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", lk_jwt_port), nil))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", lk_jwt_port), handler.prepareMux()))
}

func getJoinToken(apiKey, apiSecret, room, identity string) (string, error) {
Expand Down
104 changes: 104 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2025 New Vector Ltd

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package main

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/matrix-org/gomatrix"
)

func TestHealthcheck(t *testing.T) {
handler := &Handler{}
req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler.prepareMux().ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
}

func TestHandleOptions(t *testing.T) {
handler := &Handler{}
req, err := http.NewRequest("OPTIONS", "/sfu/get", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler.prepareMux().ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code for OPTIONS: got %v want %v", status, http.StatusOK)
}
}

func TestHandlePostMissingRoom(t *testing.T) {
handler := &Handler{}
body := SFURequest{
Room: "",
OpenIDToken: OpenIDTokenType{AccessToken: "token", MatrixServerName: "server"},
DeviceID: "device",
}
jsonBody, _ := json.Marshal(body)

req, err := http.NewRequest("POST", "/sfu/get", bytes.NewBuffer(jsonBody))
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler.prepareMux().ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}

var resp gomatrix.RespError
err = json.NewDecoder(rr.Body).Decode(&resp)
if err != nil {
t.Errorf("failed to decode response body %v", err)
}

if resp.ErrCode != "M_BAD_JSON" {
t.Errorf("unexpected error code: got %v want %v", resp.ErrCode, "M_BAD_JSON")
}
}

func TestGetJoinToken(t *testing.T) {
apiKey := "testKey"
apiSecret := "testSecret"
room := "testRoom"
identity := "[email protected]"

token, err := getJoinToken(apiKey, apiSecret, room, identity)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if token == "" {
t.Error("expected token to be non-empty")
}
}

0 comments on commit cc43c8e

Please sign in to comment.