Commits

Kaya Kupferschmidt committed e764742

Fixed MediaType and added unittest

Comments (0)

Files changed (6)

source/libs/magnum_net/magnum_net.files

 test/iflist/SConscript
 test/iflist/iflist.cpp
 test/SConscript
+source/magnum/net/MediaType.h
+source/magnum/net/MediaType.cpp
+unittest/Test_MediaType.h

source/libs/magnum_net/source/magnum/net/MediaType.cpp

 
 
 namespace magnum { namespace net {
+using magnum::types::index_t;
 
 
 /*--------------------------------------------------------------------------*/
 /*--------------------------------------------------------------------------*/
 /**
  */
-bool MediaType::isValid() const;
+bool MediaType::isValid() const
 {
     return !m_Type.isEmpty() && !m_Subtype.isEmpty();
 }
 /*--------------------------------------------------------------------------*/
 /**
  */
-CString MediaType::toCString() const;
+CString MediaType::toCString() const
 {
     return m_Type + '/' + m_Subtype;
 }
 
+
+/*--------------------------------------------------------------------------*/
+/**
+ */
+void MediaType::parse(const CString& str)
+{
+    index_t split = str.indexOf('/');
+    if (split == CString::npos) {
+        m_Type = str;
+        m_Subtype.clear();
+    }
+    else {
+        m_Type = str.substring(0, split);
+        m_Subtype = str.substring(split+1);
+    }
+}
+
 } }
-
-#endif

source/libs/magnum_net/source/magnum/net/MediaType.h

     String toString() const;
     CString toCString() const;
 
+    void parse(const CString& );
+
 private:
     CString m_Type;
     CString m_Subtype;

source/libs/magnum_net/unittest/Test_MediaType.h

+/*
+  Magnum
+  Copyright (C) 2002-2011 Kaya Kupferschmidt. All Rights Reserved.
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, under the terms in LICENSE.TXT.
+
+  Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
+*/
+
+#include "magnum/system.h"
+#include "magnum/test.h"
+#include "magnum/net/MediaType.h"
+
+
+using namespace magnum::net;
+using namespace magnum::io;
+using magnum::types::String;
+
+
+class Test_MediaType : public magnum::test::StdTestSuite {
+public:
+
+void test_empty(void)
+{
+    MediaType empty;
+    TEST_ASSERT( empty.isEmpty() );
+    TEST_ASSERT( !empty.isValid() );
+    TEST_ASSERT( empty.getType() == "" );
+    TEST_ASSERT( empty.getSubtype() == "" );
+    TEST_ASSERT( empty.toString() == "" );
+}
+
+
+void test_simple(void)
+{
+    MediaType mt("type/subtype");
+    TEST_ASSERT( !mt.isEmpty() );
+    TEST_ASSERT( mt.isValid() );
+    TEST_ASSERT( mt.getType() == "type" );
+    TEST_ASSERT( mt.getSubtype() == "subtype" );
+    TEST_ASSERT( mt.toString() == "type/subtype" );
+}
+
+
+void test_parser(void)
+{
+    MediaType mt("type","subtype");
+    TEST_ASSERT( !mt.isEmpty() );
+    TEST_ASSERT( mt.isValid() );
+    TEST_ASSERT( mt.getType() == "type" );
+    TEST_ASSERT( mt.getSubtype() == "subtype" );
+    TEST_ASSERT( mt.toString() == "type/subtype" );
+}
+
+
+Test_MediaType()
+{
+    TEST_ADD( Test_MediaType::test_empty );
+    TEST_ADD( Test_MediaType::test_simple );
+    TEST_ADD( Test_MediaType::test_parser );
+}
+};
+

source/libs/magnum_net/unittest/main.cpp

 #include "magnum/system.h"
 
 #include "Test_Address.h"
+#include "Test_MediaType.h"
 #include "Test_Subnet.h"
 #include "Test_IdnEncoding.h"
 #include "Test_Resolver.h"

source/libs/magnum_net_http/source/magnum/net/http/HttpClientConnection.cpp

         file = '/';
 
     // Build request string
-    CString cmd;
     if (request.getVersion() == HttpVersion::Http_1_0) {
         if (url.isHostNumeric())
-            cmd << request.getMethod() << ' ' << file << " HTTP/1.0\r\n";
+            *output << request.getMethod() << ' ' << file << " HTTP/1.0\r\n";
         else
-            cmd << request.getMethod() << ' ' << file << " HTTP/1.0\r\nHost: " << url.getEncodedHost() + "\r\n";
+            *output << request.getMethod() << ' ' << file << " HTTP/1.0\r\nHost: " << url.getEncodedHost() + "\r\n";
     }
     else if (request.getVersion() == HttpVersion::Http_1_1) {
-        cmd << request.getMethod() << ' ' << file << " HTTP/1.1\r\nHost: " << url.getEncodedHost()
+        *output << request.getMethod() << ' ' << file << " HTTP/1.1\r\nHost: " << url.getEncodedHost()
             << "\r\nAccept-Encoding: gzip, deflate, identity\r\n";
     }
     else {
         if (url.isHostNumeric())
-            cmd << request.getMethod() << ' ' << file << " HTTP/1.0\r\n";
+            *output << request.getMethod() << ' ' << file << " HTTP/1.0\r\n";
         else
-            cmd << request.getMethod() << ' ' << file << " HTTP/1.0\r\nHost: " << url.getEncodedHost() << "\r\n";
+            *output << request.getMethod() << ' ' << file << " HTTP/1.0\r\nHost: " << url.getEncodedHost() << "\r\n";
     }
 
     // Enable keep-alive (for http 1.0)
     if (getProxy())
-        cmd << "Proxy-Connection: Keep-Alive\r\n";
+        *output << "Proxy-Connection: Keep-Alive\r\n";
     else
-        cmd << "Connection: Keep-Alive\r\n";
-
-    // Send request to HTTP server
-    output->write(cmd.getConstData(), cmd.getLength());
+        *output << "Connection: Keep-Alive\r\n";
 
     // Write headers
     const MimeHeaders& headers = request.getHeaders();