File size: 7,176 Bytes
62cfde8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
diff --git a/services/web/app/src/Features/Spelling/SpellingController.js b/services/web/app/src/Features/Spelling/SpellingController.js
index 59008378cd..cdf28f36d9 100644
--- a/services/web/app/src/Features/Spelling/SpellingController.js
+++ b/services/web/app/src/Features/Spelling/SpellingController.js
@@ -28,40 +28,33 @@ module.exports = {
     })
   },
 
-  proxyRequestToSpellingApi(req, res) {
+  proxyCheckRequestToSpellingApi(req, res) {
     const { language } = req.body
 
-    let url = req.url.slice('/spelling'.length)
-
-    if (url === '/check') {
-      if (!language) {
-        logger.error(
-          {},
-          '"language" field should be included for spell checking'
-        )
-        return res.status(422).json({ misspellings: [] })
-      }
+    if (!language) {
+      logger.error({}, '"language" field should be included for spell checking')
+      return res.status(422).json({ misspellings: [] })
+    }
 
-      if (!languageCodeIsSupported(language)) {
-        // this log statement can be changed to 'error' once projects with
-        // unsupported languages are removed from the DB
-        logger.debug({ language }, 'language not supported')
-        return res.status(422).json({ misspellings: [] })
-      }
+    if (!languageCodeIsSupported(language)) {
+      // this log statement can be changed to 'error' once projects with
+      // unsupported languages are removed from the DB
+      logger.debug({ language }, 'language not supported')
+      return res.status(422).json({ misspellings: [] })
     }
 
     const userId = SessionManager.getLoggedInUserId(req.session)
-    url = `/user/${userId}${url}`
+    const url = `${Settings.apis.spelling.url}/user/${userId}/check`
     req.headers.Host = Settings.apis.spelling.host
     return request({
-      url: Settings.apis.spelling.url + url,
-      method: req.method,
+      url,
+      method: 'POST',
       headers: req.headers,
       json: req.body,
       timeout: TEN_SECONDS,
     })
       .on('error', function (error) {
-        logger.error({ err: error }, 'Spelling API error')
+        logger.error({ err: error }, 'Spelling Check API error')
         return res.status(500).end()
       })
       .pipe(res)
diff --git a/services/web/app/src/router.js b/services/web/app/src/router.js
index d48900bda9..4a92dcd118 100644
--- a/services/web/app/src/router.js
+++ b/services/web/app/src/router.js
@@ -1083,7 +1083,7 @@ function initialize(webRouter, privateApiRouter, publicApiRouter) {
   webRouter.post(
     '/spelling/check',
     AuthenticationController.requireLogin(),
-    SpellingController.proxyRequestToSpellingApi
+    SpellingController.proxyCheckRequestToSpellingApi
   )
   webRouter.post(
     '/spelling/learn',
diff --git a/services/web/test/unit/src/Spelling/SpellingControllerTests.js b/services/web/test/unit/src/Spelling/SpellingControllerTests.js
index ef3fe2f53a..aee062c3c9 100644
--- a/services/web/test/unit/src/Spelling/SpellingControllerTests.js
+++ b/services/web/test/unit/src/Spelling/SpellingControllerTests.js
@@ -56,12 +56,12 @@ describe('SpellingController', function () {
     this.res = new MockResponse()
   })
 
-  describe('proxyRequestToSpellingApi', function () {
+  describe('proxyCheckRequestToSpellingApi', function () {
     describe('on successful call', function () {
       beforeEach(function () {
         this.req.session.user._id = this.userId = 'user-id-123'
         this.req.body = { language: 'en', words: ['blab'] }
-        this.controller.proxyRequestToSpellingApi(this.req, this.res)
+        this.controller.proxyCheckRequestToSpellingApi(this.req, this.res)
       })
 
       it('should send a request to the spelling host', function () {
@@ -89,35 +89,19 @@ describe('SpellingController', function () {
       beforeEach(function () {
         this.req.session.user._id = this.userId = 'user-id-123'
         this.req.body = { language: 'fi', words: ['blab'] }
+        this.controller.proxyCheckRequestToSpellingApi(this.req, this.res)
       })
 
-      describe('when the request is a check request', function () {
-        beforeEach(function () {
-          this.controller.proxyRequestToSpellingApi(this.req, this.res)
-        })
-
-        it('should not send a request to the spelling host', function () {
-          this.request.called.should.equal(false)
-        })
-
-        it('should return an empty misspellings array', function () {
-          this.res.json.calledWith({ misspellings: [] }).should.equal(true)
-        })
-
-        it('should return a 422 status', function () {
-          this.res.status.calledWith(422).should.equal(true)
-        })
+      it('should not send a request to the spelling host', function () {
+        this.request.called.should.equal(false)
       })
 
-      describe('when the request is not a check request', function () {
-        beforeEach(function () {
-          this.req.url = '/spelling/learn'
-          this.controller.proxyRequestToSpellingApi(this.req, this.res)
-        })
+      it('should return an empty misspellings array', function () {
+        this.res.json.calledWith({ misspellings: [] }).should.equal(true)
+      })
 
-        it('should send a request to the spelling host', function () {
-          this.request.called.should.equal(true)
-        })
+      it('should return a 422 status', function () {
+        this.res.status.calledWith(422).should.equal(true)
       })
     })
 
@@ -125,35 +109,19 @@ describe('SpellingController', function () {
       beforeEach(function () {
         this.req.session.user._id = this.userId = 'user-id-123'
         this.req.body = { words: ['blab'] }
+        this.controller.proxyCheckRequestToSpellingApi(this.req, this.res)
       })
 
-      describe('when the request is a check request', function () {
-        beforeEach(function () {
-          this.controller.proxyRequestToSpellingApi(this.req, this.res)
-        })
-
-        it('should not send a request to the spelling host', function () {
-          this.request.called.should.equal(false)
-        })
-
-        it('should return an empty misspellings array', function () {
-          this.res.json.calledWith({ misspellings: [] }).should.equal(true)
-        })
-
-        it('should return a 422 status', function () {
-          this.res.status.calledWith(422).should.equal(true)
-        })
+      it('should not send a request to the spelling host', function () {
+        this.request.called.should.equal(false)
       })
 
-      describe('when the request is not a check request', function () {
-        beforeEach(function () {
-          this.req.url = '/spelling/learn'
-          this.controller.proxyRequestToSpellingApi(this.req, this.res)
-        })
+      it('should return an empty misspellings array', function () {
+        this.res.json.calledWith({ misspellings: [] }).should.equal(true)
+      })
 
-        it('should send a request to the spelling host', function () {
-          this.request.called.should.equal(true)
-        })
+      it('should return a 422 status', function () {
+        this.res.status.calledWith(422).should.equal(true)
       })
     })
   })