Initial import CONNEG library, support server side

content negotiation.
This commit is contained in:
jvelilla
2011-11-17 12:54:35 +01:00
committed by Jocelyn Fiat
parent 6ec37c90a2
commit b58e4e19e1
23 changed files with 2141 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
*~
EIFGEN* # ignore all files in the EIFGENs/ directory

View File

@@ -0,0 +1,87 @@
note
description : "eMIME application root class"
date : "$Date$"
revision : "$Revision$"
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
local
mime_parse : MIME_PARSE
accept : STRING
charset_parse : COMMON_ACCEPT_HEADER_PARSER
language : LANGUAGE_PARSE
do
create mime_parse
-- parse_result := mime_parse.parse_mime_type ("application/xhtml;q=0.5")
-- print ("%N"+parse_result.out)
-- parse_result := mime_parse.parse_media_range ("application/xml;q=1")
-- print ("%N"+parse_result.out)
-- check
-- "('application', 'xml', {'q':'1',})" ~ mime_parse.parse_media_range ("application/xml;q=1").out
-- end
-- parse_result := mime_parse.parse_media_range ("application/xml")
-- print ("%N"+parse_result.out)
-- check
-- "('application', 'xml', {'q':'1',})" ~ mime_parse.parse_media_range ("application/xml;q=1").out
-- end
-- assertEquals("('application', 'xml', {'q':'1',})", MIMEParse
-- .parseMediaRange("application/xml").toString());
-- assertEquals("('application', 'xml', {'q':'1',})", MIMEParse
-- .parseMediaRange("application/xml;q=").toString());
-- assertEquals("('application', 'xml', {'q':'1',})", MIMEParse
-- .parseMediaRange("application/xml ; q=").toString());
-- assertEquals("('application', 'xml', {'b':'other','q':'1',})",
-- MIMEParse.parseMediaRange("application/xml ; q=1;b=other")
-- .toString());
-- assertEquals("('application', 'xml', {'b':'other','q':'1',})",
-- MIMEParse.parseMediaRange("application/xml ; q=2;b=other")
-- .toString());
-- // Java URLConnection class sends an Accept header that includes a
-- // single *
-- assertEquals("('*', '*', {'q':'.2',})", MIMEParse.parseMediaRange(
-- " *; q=.2").toString());
accept := "application/atom+xml;q=1.0,application/xml;q=0.6,text/html"
print ("%N"+mime_parse.quality ("text/html;q=1.0", accept).out)
print ("%N"+mime_parse.quality ("application/xml", accept).out)
print ("%N"+mime_parse.quality ("*/*;q=0.1", accept).out)
accept := "application/atom+xml"
print ("%N"+mime_parse.parse_mime_type (accept).out)
create charset_parse
accept := "iso-8859-5"
print ("%N" + charset_parse.parse_common (accept).out)
accept := "unicode-1-1;q=0.8"
print ("%N" + charset_parse.parse_common (accept).out)
accept:= "iso-8859-5, unicode-1-1;q=0.8"
print ("%N"+ charset_parse.quality ("iso-8859-5", accept).out)
print ("%N"+ charset_parse.quality ("unicode-1-1", accept).out)
print ("%N"+ charset_parse.quality ("iso-8859-1", accept).out)
create language
accept :="da, en-gb;q=0.8, en;q=0.7"
print (language.best_match (accept.split (','), "da"))
print (language.best_match (accept.split (','), "en-*"))
print ("%N"+language.parse_media_range ("da").out)
print ("%N"+language.parse_media_range ("en-gb;q=0.8").out)
print ("%N"+language.parse_media_range ("en;q=0.7").out)
print ("%N"+language.parse_media_range ("en-*").out)
end
end

View File

@@ -0,0 +1,57 @@
note
description: "Summary description for {COMMON_ACCEPT_HEADER_PARSER_TEST}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
COMMON_ACCEPT_HEADER_PARSER_TEST
inherit
EQA_TEST_SET
redefine
on_prepare
end
feature {NONE} -- Events
on_prepare
-- Called after all initializations in `default_create'.
do
create parser
end
feature -- Test routines
test_parse_charsets
do
assert ("Expected ('iso-8859-5', {'q':'1.0',})", parser.parse_common("iso-8859-5").out.same_string("('iso-8859-5', {'q':'1.0',})") )
assert ("Expected ('unicode-1-1', {'q':'0.8',})", parser.parse_common("unicode-1-1;q=0.8").out.same_string("('unicode-1-1', {'q':'0.8',})") )
assert ("Expected ('*', {'q':'1.0',})", parser.parse_common("*").out.same_string("('*', {'q':'1.0',})") )
end
test_quality_example
local
accept : STRING
do
accept := "iso-8859-5, unicode-1-1;q=0.8";
assert ("Expected 1.0", 1.0 = parser.quality ("iso-8859-5", accept))
assert ("Expected 0.8", 0.8 = parser.quality ("unicode-1-1", accept))
end
test_best_match
local
charset_supported : LIST [STRING]
l_charsets : STRING
do
l_charsets := "iso-8859-5, unicode-1-1;q=0.8"
charset_supported := l_charsets.split(',')
assert ("Expected iso-8859-5", parser.best_match (charset_supported, "*").same_string ("iso-8859-5"))
assert ("Expected unicode-1-1", parser.best_match (charset_supported, "unicode-1-1;q=1").same_string ("unicode-1-1"))
end
parser : COMMON_ACCEPT_HEADER_PARSER
end

View File

@@ -0,0 +1,117 @@
note
description: "Summary description for {LANGUAGE_PARSER_TEST}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
LANGUAGE_PARSER_TEST
inherit
EQA_TEST_SET
redefine
on_prepare
end
feature {NONE} -- Events
on_prepare
-- Called after all initializations in `default_create'.
do
create parser
end
feature -- Test routines
test_parse_media_range
do
assert ("Expected ('da', {'q':'1.0',})", parser.parse_media_range ("da").out.same_string ("('da', {'q':'1.0',})"));
assert ("Expected ('en', 'gb', {'q':'0.8',})", parser.parse_media_range ("en-gb;q=0.8").out.same_string ("('en', 'gb', {'q':'0.8',})"));
assert ("Expected ('en', {'q':'0.7',})", parser.parse_media_range ("en;q=0.7").out.same_string ("('en', {'q':'0.7',})"));
assert ("Expected ('en', '*', {'q':'1.0',})", parser.parse_media_range ("en-*").out.same_string ("('en', '*', {'q':'1.0',})"));
end
test_RFC2616_example
local
accept : STRING
do
accept := "da, en-gb;q=0.8, en;q=0.7";
assert ("Expected 1.0", 1.0 = parser.quality ("da", accept))
assert ("Expected 0.8", 0.8 = parser.quality ("en-gb", accept))
assert ("Expected 0.8", 0.8 = parser.quality ("en", accept))
assert ("Expected 0.8", 0.8 = parser.quality ("en-*", accept))
end
test_best_match
local
mime_types_supported : LIST [STRING]
l_types : STRING
do
l_types := "en-gb,en-us"
mime_types_supported := l_types.split(',')
assert ("Expected en-us", parser.best_match (mime_types_supported, "en-us").same_string ("en-us"))
assert ("Direct match with a q parameter", parser.best_match (mime_types_supported, "en-gb;q=1").same_string ("en-gb"))
assert ("Direct match second choice with a q parameter", parser.best_match (mime_types_supported, "en-us;q=1").same_string ("en-us"))
assert ("Direct match using a subtype wildcard", parser.best_match (mime_types_supported, "en-*;q=1").is_equal ("en-gb"))
assert ("Match using a type wildcard", parser.best_match (mime_types_supported, "*").same_string ("en-gb"))
l_types := "en-gb,es"
mime_types_supported := l_types.split(',')
assert ("Match using a type versus a lower weighted subtype", parser.best_match (mime_types_supported, "es-*;q=0.5,*;q=0.1").same_string ("es"))
assert ("Fail to match anything",parser.best_match (mime_types_supported, "fr; q=0.9").same_string (""))
l_types := "en-gb,en-us"
mime_types_supported := l_types.split(',')
assert ("Test 1 verify fitness ordering", parser.best_match (mime_types_supported, "en-gb,en-us,*").same_string ("en-gb"))
l_types := "es,en-gb;q=1.0,fr;q=0.6"
mime_types_supported := l_types.split(',')
assert ("Match default es at first position", parser.best_match (mime_types_supported, "es;q=1.0,*;q=0.1,fr").same_string ("es"))
l_types := "en-gb;q=1.0,fr;q=0.6,es"
mime_types_supported := l_types.split(',')
assert ("Match default es at last position", parser.best_match (mime_types_supported, "es;q=1.0,*;q=0.1,fr").same_string ("es"))
l_types := "en-gb;q=1.0,fr,es"
mime_types_supported := l_types.split(',')
assert ("Match first top quality and fitness", parser.best_match (mime_types_supported, "es;q=1.0,*;q=0.1,fr").same_string ("es"))
l_types := "fr;q=1.0,en,es"
mime_types_supported := l_types.split(',')
assert ("Test 1", parser.best_match (mime_types_supported, "es;q=1.0,*/*;q=0.1,en;q=0.9").same_string ("es"))
l_types := "fr;q=1.0,en,es"
mime_types_supported := l_types.split(',')
assert ("Test 1", parser.best_match (mime_types_supported, "es,*/*;q=0.1,en").same_string ("es"))
l_types := "fr;q=1.0,en,es"
mime_types_supported := l_types.split(',')
assert ("Test 2", parser.best_match (mime_types_supported, "en,es,*/*;q=0.1").same_string ("en"))
l_types := "es,en;q=0.6"
mime_types_supported := l_types.split(',')
assert ("Test 2", parser.best_match (mime_types_supported, "fr;q=1.0, en;q=0.6, es").same_string ("es"))
end
test_support_wildcard
local
mime_types_supported : LIST[STRING]
l_types : STRING
do
l_types := "en-*,fr"
mime_types_supported := l_types.split(',')
assert ("match using a type wildcard", parser.best_match (mime_types_supported, "en-gb").same_string ("en-*"))
end
parser : LANGUAGE_PARSE
end

View File

@@ -0,0 +1,129 @@
note
description: "[
Eiffel tests that can be executed by testing tool.
]"
author: "EiffelStudio test wizard"
date: "$Date$"
revision: "$Revision$"
testing: "type/manual"
class
MIME_PARSER_TEST
inherit
EQA_TEST_SET
redefine
on_prepare
end
feature {NONE} -- Events
on_prepare
-- Called after all initializations in `default_create'.
do
create parser
end
feature -- Test routines
test_parse_media_range
do
assert ("Expected ('application', 'xml', {'q':'1',})", parser.parse_media_range("application/xml;q=1").out.same_string("('application', 'xml', {'q':'1.0',})") )
assert ("Expected ('application', 'xml', {'q':'1',})", parser.parse_media_range("application/xml").out.same_string("('application', 'xml', {'q':'1.0',})") )
assert ("Expected ('application', 'xml', {'q':'1',})", parser.parse_media_range("application/xml;q=").out.same_string("('application', 'xml', {'q':'1.0',})") )
assert ("Expected ('application', 'xml', {'q':'1',})", parser.parse_media_range("application/xml ; q=").out.same_string("('application', 'xml', {'q':'1.0',})") )
assert ("Expected ('application', 'xml', {'q':'1','b':'other',})", parser.parse_media_range("application/xml ; q=1;b=other").out.same_string("('application', 'xml', {'q':'1.0','b':'other',})") )
assert ("Expected ('application', 'xml', {'q':'1','b':'other',})", parser.parse_media_range("application/xml ; q=2;b=other").out.same_string("('application', 'xml', {'q':'1.0','b':'other',})") )
-- Accept header that includes *
assert ("Expected ('*', '*', {'q':'.2',})", parser.parse_media_range(" *; q=.2").out.same_string("('*', '*', {'q':'.2',})"))
end
test_RFC2616_example
local
accept : STRING
do
accept := "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5";
assert ("Expected 1.0", 1.0 = parser.quality ("text/html;level=1", accept))
assert ("Expected 0.3", 0.3 = parser.quality ("text/plain", accept))
assert ("Expected 0.7", 0.7 = parser.quality ("text/html", accept))
assert ("Expected 0.5", 0.5 = parser.quality ("image/jpeg", accept))
assert ("Expected 0.4", 0.4 = parser.quality ("text/html;level=2", accept))
assert ("Expected 0.7", 0.7 = parser.quality ("text/html;level=3", accept))
end
test_best_match
local
mime_types_supported : LIST [STRING]
l_types : STRING
do
l_types := "application/xbel+xml,application/xml"
mime_types_supported := l_types.split(',')
assert ("Expected application/xbel+xml", parser.best_match (mime_types_supported, "application/xbel+xml").same_string ("application/xbel+xml"))
assert ("Direct match with a q parameter", parser.best_match (mime_types_supported, "application/xbel+xml;q=1").same_string ("application/xbel+xml"))
assert ("Direct match second choice with a q parameter", parser.best_match (mime_types_supported, "application/xml;q=1").same_string ("application/xml"))
assert ("Direct match using a subtype wildcard", parser.best_match (mime_types_supported, "application/*;q=1").is_equal ("application/xbel+xml"))
assert ("Match using a type wildcard", parser.best_match (mime_types_supported, "*/*").same_string ("application/xbel+xml"))
l_types := "application/xbel+xml,text/xml"
mime_types_supported := l_types.split(',')
assert ("Match using a type versus a lower weighted subtype", parser.best_match (mime_types_supported, "text/*;q=0.5,*/*;q=0.1").same_string ("text/xml"))
assert ("Fail to match anything",parser.best_match (mime_types_supported, "text/html,application/atom+xml; q=0.9").same_string (""))
l_types := "application/json,text/html"
mime_types_supported := l_types.split(',')
assert ("Common Ajax scenario", parser.best_match (mime_types_supported, "application/json,text/javascript, */*").same_string ("application/json"))
assert ("Common Ajax scenario,verify fitness ordering", parser.best_match (mime_types_supported, "application/json,text/javascript, */*").same_string ("application/json"))
l_types := "text/html,application/atom+xml;q=1.0,application/xml;q=0.6"
mime_types_supported := l_types.split(',')
assert ("Match default text/html at first position", parser.best_match (mime_types_supported, "text/html;q=1.0,*/*;q=0.1,application/xml").same_string ("text/html"))
l_types := "application/atom+xml;q=1.0,application/xml;q=0.6,text/html"
mime_types_supported := l_types.split(',')
assert ("Match default text/html at last position", parser.best_match (mime_types_supported, "text/html;q=1.0,*/*;q=0.1,application/xml").same_string ("text/html"))
l_types := "application/atom+xml;q=1.0,application/xml,text/html"
mime_types_supported := l_types.split(',')
assert ("Match first top quality and fitness", parser.best_match (mime_types_supported, "text/html;q=1.0,*/*;q=0.1,application/xml").same_string ("text/html"))
l_types := "application/atom+xml;q=1.0,application/xml,text/html"
mime_types_supported := l_types.split(',')
assert ("Test 1", parser.best_match (mime_types_supported, "text/html;q=1.0,*/*;q=0.1,application/xml;q=0.9").same_string ("text/html"))
l_types := "application/atom+xml;q=1.0,application/xml,text/html"
mime_types_supported := l_types.split(',')
assert ("Test 1", parser.best_match (mime_types_supported, "text/html,*/*;q=0.1,application/xml").same_string ("text/html"))
l_types := "application/atom+xml;q=1.0,application/xml,text/html"
mime_types_supported := l_types.split(',')
assert ("Test 2", parser.best_match (mime_types_supported, "application/xml,text/html,*/*;q=0.1").same_string ("application/xml"))
l_types := "text/html,application/xml;q=0.6"
mime_types_supported := l_types.split(',')
assert ("Test 2", parser.best_match (mime_types_supported, "application/atom+xml;q=1.0, application/xml;q=0.6, text/html").same_string ("text/html"))
end
test_support_wildcard
local
mime_types_supported : LIST[STRING]
l_types : STRING
do
l_types := "image/*,application/xml"
mime_types_supported := l_types.split(',')
assert ("match using a type wildcard", parser.best_match (mime_types_supported, "image/png").same_string ("image/*"))
assert ("match using a wildcard for both requested and supported", parser.best_match (mime_types_supported, "image/*").same_string ("image/*"))
end
parser : MIME_PARSE
end

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-6-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-6-0 http://www.eiffel.com/developers/xml/configuration-1-6-0.xsd" name="test" uuid="EA141B17-6A21-4781-8B5F-E9939BAE968A">
<target name="test">
<root class="APPLICATION" feature="make"/>
<option warning="true">
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
</option>
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
<library name="conneg" location="..\library\conneg-safe.ecf"/>
<library name="testing" location="$ISE_LIBRARY\library\testing\testing-safe.ecf"/>
<cluster name="test" location=".\" recursive="true">
<file_rule>
<exclude>/EIFGENs$</exclude>
<exclude>/CVS$</exclude>
<exclude>/.svn$</exclude>
<exclude>/.git$</exclude>
</file_rule>
</cluster>
</target>
</system>

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-8-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-8-0 http://www.eiffel.com/developers/xml/configuration-1-8-0.xsd" name="test" uuid="EA141B17-6A21-4781-8B5F-E9939BAE968A">
<target name="test">
<root class="APPLICATION" feature="make"/>
<option warning="true">
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
</option>
<library name="base" location="$ISE_LIBRARY\library\base\base.ecf"/>
<library name="base_extension" location="$ISE_LIBRARY\library\base_extension\base_extension-safe.ecf"/>
<library name="conneg" location="..\library\conneg.ecf"/>
<library name="testing" location="$ISE_LIBRARY\library\testing\testing.ecf"/>
<cluster name="test" location=".\" recursive="true">
<file_rule>
<exclude>/.git$</exclude>
<exclude>/EIFGENs$</exclude>
<exclude>/CVS$</exclude>
<exclude>/.svn$</exclude>
</file_rule>
</cluster>
</target>
</system>