summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJuan Carlos <juancarlospaco@gmail.com>2019-02-12 03:22:04 -0300
committerVarriount <Varriount@users.noreply.github.com>2019-02-12 01:22:04 -0500
commit33ddc2105779938da7f8f85ec97704841f9a8fee (patch)
tree3a922a7eda8f97e277c96c7026c0bd67f46d6d5c
parent85f2ab1c43e846ec99014d8f0901f0ea65876908 (diff)
downloadNim-33ddc2105779938da7f8f85ec97704841f9a8fee.tar.gz
Lowercase ext before querying the mimedb on mimetypes module, change isMainModule to runnableExamples (#10644)
-rw-r--r--lib/pure/mimetypes.nim40
1 files changed, 29 insertions, 11 deletions
diff --git a/lib/pure/mimetypes.nim b/lib/pure/mimetypes.nim
index 95c51487a..57ea82527 100644
--- a/lib/pure/mimetypes.nim
+++ b/lib/pure/mimetypes.nim
@@ -9,6 +9,8 @@
 
 ## This module implements a mimetypes database
 import strtabs
+from strutils import startsWith, toLowerAscii, strip
+
 type
   MimeDB* = object
     mimes: StringTableRef
@@ -1878,37 +1880,53 @@ const mimes* = {
   "~": "application/x-trash"
 }
 
-from strutils import startsWith
 
-proc newMimetypes*(): MimeDB =
+func newMimetypes*(): MimeDB =
   ## Creates a new Mimetypes database. The database will contain the most
   ## common mimetypes.
   result.mimes = mimes.newStringTable()
 
-proc getMimetype*(mimedb: MimeDB, ext: string, default = "text/plain"): string =
+func getMimetype*(mimedb: MimeDB, ext: string, default = "text/plain"): string =
   ## Gets mimetype which corresponds to ``ext``. Returns ``default`` if ``ext``
   ## could not be found. ``ext`` can start with an optional dot which is ignored.
+  ## ``ext`` is lowercased before querying ``mimedb``.
   if ext.startsWith("."):
-    result = mimedb.mimes.getOrDefault(ext.substr(1))
+    result = mimedb.mimes.getOrDefault(ext.toLowerAscii.substr(1))
   else:
-    result = mimedb.mimes.getOrDefault(ext)
+    result = mimedb.mimes.getOrDefault(ext.toLowerAscii())
   if result == "":
     return default
 
-proc getExt*(mimedb: MimeDB, mimetype: string, default = "txt"): string =
+func getExt*(mimedb: MimeDB, mimetype: string, default = "txt"): string =
   ## Gets extension which corresponds to ``mimetype``. Returns ``default`` if
   ## ``mimetype`` could not be found. Extensions are returned without the
-  ## leading dot.
+  ## leading dot. ``mimetype`` is lowercased before querying ``mimedb``.
   result = default
+  let mimeLowered = mimetype.toLowerAscii()
   for e, m in mimedb.mimes:
-    if m == mimetype:
+    if m == mimeLowered:
       result = e
 
-proc register*(mimedb: var MimeDB, ext: string, mimetype: string) =
+func register*(mimedb: var MimeDB, ext: string, mimetype: string) =
   ## Adds ``mimetype`` to the ``mimedb``.
-  mimedb.mimes[ext] = mimetype
+  ## ``mimetype`` and ``ext`` are lowercased before registering on ``mimedb``.
+  assert ext.strip.len > 0, "ext argument can not be empty string"
+  assert mimetype.strip.len > 0, "mimetype argument can not be empty string"
+  mimedb.mimes[ext.toLowerAscii()] = mimetype.toLowerAscii()
 
-when isMainModule:
+runnableExamples:
   var m = newMimetypes()
   assert m.getMimetype("mp4") == "video/mp4"
   assert m.getExt("text/html") == "html"
+  ## Values can be uppercase too.
+  assert m.getMimetype("MP4") == "video/mp4"
+  assert m.getExt("TEXT/HTML") == "html"
+  ## If values are invalid then ``default`` is returned.
+  assert m.getMimetype("INVALID") == "text/plain"
+  assert m.getExt("INVALID/NONEXISTENT") == "txt"
+  assert m.getMimetype("") == "text/plain"
+  assert m.getExt("") == "txt"
+  ## Register new Mimetypes.
+  m.register(ext="fakext", mimetype="text/fakelang")
+  assert m.getMimetype("fakext") == "text/fakelang"
+  assert m.getMimetype("FaKeXT") == "text/fakelang"