OSDN Git Service

エラーログからトークンを削除する正規表現を見直す
authorKazuhiro Fujieda <fujieda@users.osdn.me>
Mon, 4 Dec 2017 12:09:17 +0000 (21:09 +0900)
committerKazuhiro Fujieda <fujieda@users.osdn.me>
Fri, 8 Dec 2017 12:51:50 +0000 (21:51 +0900)
KancolleSniffer.Test/ErrorLogTest.cs [new file with mode: 0644]
KancolleSniffer.Test/KancolleSniffer.Test.csproj
KancolleSniffer/ErrorLog.cs

diff --git a/KancolleSniffer.Test/ErrorLogTest.cs b/KancolleSniffer.Test/ErrorLogTest.cs
new file mode 100644 (file)
index 0000000..43e63ec
--- /dev/null
@@ -0,0 +1,42 @@
+// Copyright (C) 2017 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
+//\r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+//\r
+//    http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+\r
+using ExpressionToCodeLib;\r
+using Microsoft.VisualStudio.TestTools.UnitTesting;\r
+\r
+namespace KancolleSniffer.Test\r
+{\r
+    [TestClass]\r
+    public class ErrorLogTest\r
+    {\r
+        [TestMethod]\r
+        public void RemoveTokenFromRequest()\r
+        {\r
+            var request =\r
+                "api%5Fverno=1&api%5Ftoken=0123456abcdef&api%5Fport=0123456789";\r
+            var response = "";\r
+            ErrorLog.RemoveUnwantedInformation(ref request, ref response);\r
+            PAssert.That(() => request == "api%5Fverno=1&api%5Fport=0123456789", "トークンが中間");\r
+            var request2 = "api%5Fverno=1&api%5Ftoken=0123456abcdef";\r
+            ErrorLog.RemoveUnwantedInformation(ref request2, ref response);\r
+            PAssert.That(() => request2 == @"api%5Fverno=1", "トークンが末尾");\r
+            var request3 = "api%5Ftoken=0123456abcdef&api%5Fverno=1";\r
+            ErrorLog.RemoveUnwantedInformation(ref request3, ref response);\r
+            PAssert.That(() => request3 == @"api%5Fverno=1", "トークンが先頭");\r
+            var request4 = "api%5Ftoken=0123456abcdef";\r
+            ErrorLog.RemoveUnwantedInformation(ref request4, ref response);\r
+            PAssert.That(() => request4 == "", "トークン単独");\r
+        }\r
+    }\r
+}
\ No newline at end of file
index f719695..b7d5c54 100644 (file)
@@ -64,6 +64,7 @@
     <Compile Include="Ascii85Test.cs" />\r
     <Compile Include="BattleLogProcessorTest.cs" />\r
     <Compile Include="BattleTest.cs" />\r
+    <Compile Include="ErrorLogTest.cs" />\r
     <Compile Include="JsonTest.cs" />\r
     <Compile Include="ShipLabelTest.cs" />\r
     <Compile Include="SnifferTest.cs" />\r
index 0fc952a..2977ba8 100644 (file)
@@ -110,9 +110,9 @@ namespace KancolleSniffer
             return result;\r
         }\r
 \r
-        private void RemoveUnwantedInformation(ref string request, ref string response)\r
+        public static void RemoveUnwantedInformation(ref string request, ref string response)\r
         {\r
-            var token = new Regex("&api%5Ftoken=[^&]*|api%5Ftoken=[^&]*&?");\r
+            var token = new Regex("&api%5Ftoken=.+?(?=&|$)|api%5Ftoken=.+?(?:&|$)");\r
             request = token.Replace(request, "");\r
             var id = new Regex(@"""api_member_id"":\d+,?|""api_nickname"":[^,]+,""api_nickname_id"":""d+"",?");\r
             response = id.Replace(response, "");\r