Skip to content

Commit

Permalink
test(http-proxy-service): add basic testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Onyxmoon committed Nov 3, 2023
1 parent 0a2bbcf commit 57557bd
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 2 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/run-tests-http-proxy-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Run tests (http proxy service)

on:
push:
paths:
- 'src/http-proxy-service/**'
workflow_dispatch:

jobs:
test:
name: Test http-proxy-service
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21

- name: Test Go Module
run: |
cd src/http-proxy-service
go test ./...
test_exit_code=$? # Capture the exit code of the go test command
if [ $test_exit_code -eq 0 ]; then
echo "Tests passed successfully."
else
echo "Tests failed with exit code $test_exit_code."
exit 1 # Fail the GitHub Actions workflow
fi
2 changes: 1 addition & 1 deletion src/http-proxy-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func main() {
proxyConfig := proxyutils.ReadDefaultProxyManagerConfiguration("./config", "proxyConfig")
proxyConfig := proxyutils.DefaultProxyManagerConfigurationReader("./config", "proxyConfig")
proxyManager := proxy.NewDefaultManager(proxyConfig)

log.Printf("Listening on %v", proxyConfig.ListenAddress)
Expand Down
189 changes: 189 additions & 0 deletions src/http-proxy-service/proxy/default_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package proxy

import (
"io"
"net/http"
"net/http/httptest"
"testing"
)

func TestNewDefaultManager(t *testing.T) {
config := &Config{
ListenAddress: "localhost:8080",
ProxyRoutes: []Route{
{
Name: "TestRoute",
Context: "/test",
Target: "http://example.com",
},
},
}

// Create a new manager
manager := NewDefaultManager(config)

if manager == nil {
t.Error("Expected a non-nil manager, got nil")
}

// You can write more specific tests based on your requirements.
}

func TestDefaultManager_GetProxyRouter(t *testing.T) {
config := &Config{
ListenAddress: "localhost:8080",
ProxyRoutes: []Route{
{
Name: "TestRoute",
Context: "/test",
Target: "http://example.com",
},
},
}
manager := NewDefaultManager(config)

router := manager.GetProxyRouter()

if router == nil {
t.Error("Expected a non-nil router, got nil")
}
}

func TestProxyServer(t *testing.T) {
want1 := "I'm service 1"
want2 := "I'm service 2"

testService1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(want1))
}))
defer testService1.Close()
testService2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(want2))
}))
defer testService2.Close()

// Create a sample configuration for testing
config := &Config{
ListenAddress: "localhost:8080",
ProxyRoutes: []Route{
{
Name: "Route1",
Context: "/context1",
Target: testService1.URL,
},
{
Name: "Route2",
Context: "/context2",
Target: testService2.URL,
},
},
}

// Start a test HTTP server using the NewDefaultManager
proxyManager := NewDefaultManager(config)
testProxyServer := httptest.NewServer(proxyManager.GetProxyRouter())
defer testProxyServer.Close()

// Test an HTTP request to Route1
resp, err := testProxyServer.Client().Get(testProxyServer.URL + "/context1/")
if err != nil {
t.Errorf("HTTP GET to /context1/test failed: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// Check if the response body matches the expected value for Route1
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Failed to read response body: %v", err)
}
if string(body) != want1 {
t.Errorf("Expected response body for Route1 to be %s, but got %s", want1, string(body))
}

// Test an HTTP request to Route2
resp, err = testProxyServer.Client().Get(testProxyServer.URL + "/context2/")
if err != nil {
t.Errorf("HTTP GET to /context2/test failed: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}

// Check if the response body matches the expected value for Route2
body, err = io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Failed to read response body: %v", err)
}
if string(body) != want2 {
t.Errorf("Expected response body for Route2 to be %s, but got %s", want2, string(body))
}
}

/*func TestDefaultManagerNewHandler(t *testing.T) {
type fields struct {
proxies []*httputil.ReverseProxy
routing *router.Router
}
type args struct {
p *httputil.ReverseProxy
}
tests := []struct {
name string
fields fields
args args
want func(http.ResponseWriter, *http.Request)
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dp := defaultManager{
proxies: tt.fields.proxies,
routing: tt.fields.routing,
}
if got := dp.newHandler(tt.args.p); !reflect.DeepEqual(got, tt.want) {
t.Errorf("newHandler() = %v, want %v", got, tt.want)
}
})
}
}
func TestDefaultManagerNewProxy(t *testing.T) {
type fields struct {
proxies []*httputil.ReverseProxy
routing *router.Router
}
type args struct {
targetUrl string
}
tests := []struct {
name string
fields fields
args args
want *httputil.ReverseProxy
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dp := defaultManager{
proxies: tt.fields.proxies,
routing: tt.fields.routing,
}
got, err := dp.newProxy(tt.args.targetUrl)
if (err != nil) != tt.wantErr {
t.Errorf("newProxy() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("newProxy() got = %v, want %v", got, tt.want)
}
})
}
}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
)

func ReadDefaultProxyManagerConfiguration(path string, filename string) *proxy.Config {
func DefaultProxyManagerConfigurationReader(path string, filename string) *proxy.Config {
viper.AddConfigPath(path)
viper.SetConfigType("yaml")
viper.SetConfigName(filename)
Expand Down

0 comments on commit 57557bd

Please sign in to comment.