Initial commit to Proxyfier
This commit is contained in:
67
main_test.go
Normal file
67
main_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testConfig() *Config {
|
||||
return &Config{
|
||||
Listen: "127.0.0.1:0",
|
||||
Auth: AuthConfig{
|
||||
User: "user",
|
||||
Pass: "pass",
|
||||
},
|
||||
Credentials: map[string]Credential{
|
||||
"telegram": {
|
||||
Username: "tg-user",
|
||||
Password: "tg-pass",
|
||||
Note: "note",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthOK(t *testing.T) {
|
||||
mux := newMux(testConfig())
|
||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCredsUnauthorized(t *testing.T) {
|
||||
mux := newMux(testConfig())
|
||||
req := httptest.NewRequest(http.MethodGet, "/creds?service=telegram", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("expected 401, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCredsSuccess(t *testing.T) {
|
||||
mux := newMux(testConfig())
|
||||
req := httptest.NewRequest(http.MethodGet, "/creds?service=telegram", nil)
|
||||
req.SetBasicAuth("user", "pass")
|
||||
rr := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", rr.Code)
|
||||
}
|
||||
|
||||
var resp Response
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("invalid json: %v", err)
|
||||
}
|
||||
if resp.Service != "telegram" || resp.Username != "tg-user" || resp.Password != "tg-pass" {
|
||||
t.Fatalf("unexpected response: %+v", resp)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user