| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package http |
| 16 |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "testing" |
| 20 | "time" |
| 21 |
| 22 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 23 | "github.com/fatedier/frp/pkg/proto/wire" |
| 24 | "github.com/fatedier/frp/server/registry" |
| 25 | ) |
| 26 |
| 27 | func TestGetConfFromConfigurerKeepsPluginFields(t *testing.T) { |
| 28 | cfg := &v1.TCPProxyConfig{ |
| 29 | ProxyBaseConfig: v1.ProxyBaseConfig{ |
| 30 | Name: "test-proxy", |
| 31 | Type: string(v1.ProxyTypeTCP), |
| 32 | ProxyBackend: v1.ProxyBackend{ |
| 33 | Plugin: v1.TypedClientPluginOptions{ |
| 34 | Type: v1.PluginHTTPProxy, |
| 35 | ClientPluginOptions: &v1.HTTPProxyPluginOptions{ |
| 36 | Type: v1.PluginHTTPProxy, |
| 37 | HTTPUser: "user", |
| 38 | HTTPPassword: "password", |
| 39 | }, |
| 40 | }, |
| 41 | }, |
| 42 | }, |
| 43 | RemotePort: 6000, |
| 44 | } |
| 45 |
| 46 | content, err := json.Marshal(getConfFromConfigurer(cfg)) |
| 47 | if err != nil { |
| 48 | t.Fatalf("marshal conf failed: %v", err) |
| 49 | } |
| 50 |
| 51 | var out map[string]any |
| 52 | if err := json.Unmarshal(content, &out); err != nil { |
| 53 | t.Fatalf("unmarshal conf failed: %v", err) |
| 54 | } |
| 55 |
| 56 | pluginValue, ok := out["plugin"] |
| 57 | if !ok { |
| 58 | t.Fatalf("plugin field missing in output: %v", out) |
| 59 | } |
| 60 | plugin, ok := pluginValue.(map[string]any) |
| 61 | if !ok { |
| 62 | t.Fatalf("plugin field should be object, got: %#v", pluginValue) |
| 63 | } |
| 64 |
| 65 | if got := plugin["type"]; got != v1.PluginHTTPProxy { |
| 66 | t.Fatalf("plugin type mismatch, want %q got %#v", v1.PluginHTTPProxy, got) |
| 67 | } |
| 68 | if got := plugin["httpUser"]; got != "user" { |
| 69 | t.Fatalf("plugin httpUser mismatch, want %q got %#v", "user", got) |
| 70 | } |
| 71 | if got := plugin["httpPassword"]; got != "password" { |
| 72 | t.Fatalf("plugin httpPassword mismatch, want %q got %#v", "password", got) |
| 73 | } |
| 74 | } |
| 75 |
| 76 | func TestBuildClientInfoRespIncludesWireProtocol(t *testing.T) { |
| 77 | info := registry.ClientInfo{ |
| 78 | Key: "user.client", |
| 79 | User: "user", |
| 80 | RawClientID: "client", |
| 81 | RunID: "run-id", |
| 82 | Version: "1.0.0", |
| 83 | WireProtocol: wire.ProtocolV2, |
| 84 | Hostname: "host", |
| 85 | IP: "127.0.0.1", |
| 86 | FirstConnectedAt: time.Unix(1, 0), |
| 87 | LastConnectedAt: time.Unix(2, 0), |
| 88 | Online: true, |
| 89 | } |
| 90 |
| 91 | resp := buildClientInfoResp(info) |
| 92 | if resp.WireProtocol != wire.ProtocolV2 { |
| 93 | t.Fatalf("wire protocol mismatch, want %q got %q", wire.ProtocolV2, resp.WireProtocol) |
| 94 | } |
| 95 | } |