/*
 * Copyright (c) 2025-2026 IB-Robot Group & openEuler Embedded SIG & openharmony-robot sig_RoboFrame.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

#include <string>

#include "base/url_encode.h"

namespace ibmw::common::util {
namespace {

using namespace std::string_literals;

TEST(UrlEncodeTest, RoundTrip)
{
    const std::string raw = "a b/c?d=e&f\x01\xff~";
    EXPECT_EQ(UrlDecode(UrlEncode(raw)), raw);
    EXPECT_EQ(UrlDecode(UrlEncode(raw, false)), raw);
    EXPECT_EQ(HttpHeaderDecode(HttpHeaderEncode(raw)), raw);
    EXPECT_EQ(UrlDecode("%41%6a%00"), "Aj\0"s);
}

// API-041: a '%' that does not start a valid escape is kept literally instead
// of decoding to NUL or dropping the rest of the input.
TEST(UrlEncodeTest, InvalidEscapesAreKeptLiterally)
{
    for (auto decode : {&UrlDecode, &HttpHeaderDecode}) {
        EXPECT_EQ(decode("a%zzb"), "a%zzb");
        EXPECT_EQ(decode("a%4gb"), "a%4gb");
        EXPECT_EQ(decode("%g1%41"), "%g1A");
        EXPECT_EQ(decode("ab%4"), "ab%4");
        EXPECT_EQ(decode("ab%"), "ab%");
        EXPECT_EQ(decode("%%41"), "%A");
    }
}

} // namespace
} // namespace ibmw::common::util