1//
2// MediaTypeTest.cpp
3//
4// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "MediaTypeTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Net/MediaType.h"
15
16
17using Poco::Net::MediaType;
18
19
20MediaTypeTest::MediaTypeTest(const std::string& name): CppUnit::TestCase(name)
21{
22}
23
24
25MediaTypeTest::~MediaTypeTest()
26{
27}
28
29
30void MediaTypeTest::testParse()
31{
32 MediaType mt1("text/plain");
33 assertTrue (mt1.getType() == "text");
34 assertTrue (mt1.getSubType() == "plain");
35 assertTrue (mt1.parameters().empty());
36
37 MediaType mt2("text/xml;charset=us-ascii");
38 assertTrue (mt2.getType() == "text");
39 assertTrue (mt2.getSubType() == "xml");
40 assertTrue (!mt2.parameters().empty());
41 assertTrue (mt2.getParameter("charset") == "us-ascii");
42
43 MediaType mt3("application/test; param1=value1; param2=\"value 2\"");
44 assertTrue (mt3.getType() == "application");
45 assertTrue (mt3.getSubType() == "test");
46 assertTrue (!mt3.parameters().empty());
47 assertTrue (mt3.getParameter("param1") == "value1");
48 assertTrue (mt3.getParameter("PARAM2") == "value 2");
49}
50
51
52void MediaTypeTest::testToString()
53{
54 MediaType mt1("text", "plain");
55 assertTrue (mt1.toString() == "text/plain");
56
57 mt1.setParameter("charset", "iso-8859-1");
58 assertTrue (mt1.toString() == "text/plain; charset=iso-8859-1");
59
60 MediaType mt2("application", "test");
61 mt2.setParameter("param1", "value1");
62 mt2.setParameter("param2", "value 2");
63 assertTrue (mt2.toString() == "application/test; param1=value1; param2=\"value 2\"");
64}
65
66
67void MediaTypeTest::testMatch()
68{
69 MediaType mt1("Text/Plain");
70 MediaType mt2("text/plain");
71 MediaType mt3("text/xml");
72 assertTrue (mt1.matches(mt2));
73 assertTrue (!mt1.matches(mt3));
74 assertTrue (mt1.matches("text"));
75 assertTrue (mt2.matches("text"));
76 assertTrue (mt3.matches("text"));
77}
78
79
80void MediaTypeTest::testMatchRange()
81{
82 MediaType mt1("Text/Plain");
83 MediaType mt2("text/plain");
84 MediaType mt3("text/xml");
85 MediaType mt4("image/jpg");
86 MediaType mt5("text/*");
87 MediaType mt6("*/*");
88 assertTrue (mt1.matchesRange(mt5));
89 assertTrue (mt2.matchesRange(mt5));
90 assertTrue (mt3.matchesRange(mt5));
91 assertTrue (!mt4.matchesRange(mt5));
92 assertTrue (mt1.matchesRange(mt6));
93 assertTrue (mt2.matchesRange(mt6));
94 assertTrue (mt3.matchesRange(mt6));
95 assertTrue (mt4.matchesRange(mt6));
96
97 assertTrue (mt5.matchesRange(mt1));
98 assertTrue (mt5.matchesRange(mt2));
99 assertTrue (mt5.matchesRange(mt3));
100 assertTrue (!mt5.matchesRange(mt4));
101
102 assertTrue (mt1.matchesRange("text", "*"));
103 assertTrue (mt2.matchesRange("text", "*"));
104 assertTrue (mt3.matchesRange("text", "*"));
105 assertTrue (!mt4.matchesRange("text", "*"));
106
107 assertTrue (mt1.matchesRange("*"));
108 assertTrue (mt4.matchesRange("*"));
109}
110
111
112void MediaTypeTest::setUp()
113{
114}
115
116
117void MediaTypeTest::tearDown()
118{
119}
120
121
122CppUnit::Test* MediaTypeTest::suite()
123{
124 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MediaTypeTest");
125
126 CppUnit_addTest(pSuite, MediaTypeTest, testParse);
127 CppUnit_addTest(pSuite, MediaTypeTest, testToString);
128 CppUnit_addTest(pSuite, MediaTypeTest, testMatch);
129 CppUnit_addTest(pSuite, MediaTypeTest, testMatchRange);
130
131 return pSuite;
132}
133