Files
xiaomihome/miot/web_pages_test.go
T
4566704 3a44cb9e6a feat: 初始化小米 IoT (MIoT) 智能家居 Go 库
- 实现 MIoT 客户端核心功能(MQTT 连接、设备管理、属性读写)
- 支持云端 API 调用与局域网设备发现(mDNS)
- 集成国际化(i18n)多语言支持
- 添加 MIoT 设备规约解析器(spec_parser)
- 包含单元测试与使用示例
2026-06-28 22:05:49 +08:00

122 lines
3.8 KiB
Go

// Package miot_test provides black-box tests for web_pages.go page templates.
package miot_test
import (
"strings"
"testing"
miot "miot"
)
// ============================================================================
// OAuthRedirectPage
// ============================================================================
func TestOAuthRedirectPage_Success(t *testing.T) {
html := miot.OAuthRedirectPage("My Title", "Welcome!", "Close", true)
if html == "" {
t.Fatal("OAuthRedirectPage returned empty string")
}
if !strings.Contains(html, "My Title") {
t.Error("HTML should contain 'My Title'")
}
if !strings.Contains(html, "Welcome!") {
t.Error("HTML should contain 'Welcome!'")
}
if !strings.Contains(html, "Close") {
t.Error("HTML should contain 'Close'")
}
// STATUS_PLACEHOLDER should be "true"
if !strings.Contains(html, "true") {
t.Error("HTML should contain 'true' for success")
}
// Note: TITLE_PLACEHOLDER appears twice in template but strings.Replace(...,1)
// only replaces the first occurrence (in <title>). The second occurrence
// (in <a id="titleArea">) remains.
// CONTENT_PLACEHOLDER and BUTTON_PLACEHOLDER each appear once and are replaced.
if strings.Contains(html, "CONTENT_PLACEHOLDER") {
t.Error("CONTENT_PLACEHOLDER should have been replaced")
}
if strings.Contains(html, "BUTTON_PLACEHOLDER") {
t.Error("BUTTON_PLACEHOLDER should have been replaced")
}
if strings.Contains(html, "STATUS_PLACEHOLDER") {
t.Error("STATUS_PLACEHOLDER should have been replaced")
}
}
func TestOAuthRedirectPage_Failure(t *testing.T) {
html := miot.OAuthRedirectPage("Error", "Auth failed", "Retry", false)
if html == "" {
t.Fatal("OAuthRedirectPage returned empty string")
}
// STATUS_PLACEHOLDER should be "false"
if !strings.Contains(html, "false") {
t.Error("HTML should contain 'false' for failure")
}
}
func TestOAuthRedirectPage_EmptyValues(t *testing.T) {
html := miot.OAuthRedirectPage("", "", "", false)
if html == "" {
t.Fatal("OAuthRedirectPage with empty values should still return HTML")
}
// Note: TITLE_PLACEHOLDER appears twice, only first is replaced.
// CONTENT_PLACEHOLDER, BUTTON_PLACEHOLDER, STATUS_PLACEHOLDER should be gone.
if strings.Contains(html, "CONTENT_PLACEHOLDER") {
t.Error("CONTENT_PLACEHOLDER should not remain")
}
if strings.Contains(html, "BUTTON_PLACEHOLDER") {
t.Error("BUTTON_PLACEHOLDER should not remain")
}
if strings.Contains(html, "STATUS_PLACEHOLDER") {
t.Error("STATUS_PLACEHOLDER should not remain")
}
}
func TestOAuthRedirectPage_SpecialCharacters(t *testing.T) {
title := "Test <>&\"' Title"
html := miot.OAuthRedirectPage(title, "content", "button", true)
if html == "" {
t.Fatal("OAuthRedirectPage returned empty string")
}
if !strings.Contains(html, title) {
t.Error("HTML should contain the special char title")
}
}
func TestOAuthRedirectPage_LongContent(t *testing.T) {
longTitle := strings.Repeat("A", 1000)
longContent := strings.Repeat("B", 5000)
html := miot.OAuthRedirectPage(longTitle, longContent, "OK", true)
if !strings.Contains(html, longTitle) {
t.Error("HTML should contain long title")
}
if !strings.Contains(html, longContent) {
t.Error("HTML should contain long content")
}
}
func TestOAuthRedirectPage_NonExhaustivePlaceholders(t *testing.T) {
// Verify placeholder replacement behavior
html := miot.OAuthRedirectPage("T", "C", "B", true)
// These appear once and should be fully replaced
if strings.Contains(html, "CONTENT_PLACEHOLDER") {
t.Error("CONTENT_PLACEHOLDER should be replaced")
}
if strings.Contains(html, "BUTTON_PLACEHOLDER") {
t.Error("BUTTON_PLACEHOLDER should be replaced")
}
if strings.Contains(html, "STATUS_PLACEHOLDER") {
t.Error("STATUS_PLACEHOLDER should be replaced")
}
// Note: TITLE_PLACEHOLDER has 2 occurrences, only first is replaced
}