File tree Expand file tree Collapse file tree 3 files changed +21
-4
lines changed
Expand file tree Collapse file tree 3 files changed +21
-4
lines changed Original file line number Diff line number Diff line change 1+ // Copyright 2025 DeepL SE (https://www.deepl.com)
2+ // Use of this source code is governed by an MIT
3+ // license that can be found in the LICENSE file.
14package com .deepl .api ;
25
36public enum DeepLApiVersion {
Original file line number Diff line number Diff line change @@ -42,20 +42,24 @@ public class Translator {
4242 */
4343 @ Deprecated
4444 public Translator (String authKey , TranslatorOptions options ) throws IllegalArgumentException {
45- if (authKey == null || authKey .length () == 0 ) {
46- throw new IllegalArgumentException ("authKey must be a non-empty string " );
45+ if (authKey == null || authKey .isEmpty () ) {
46+ throw new IllegalArgumentException ("authKey cannot be null or empty " );
4747 }
48+
49+ String sanitizedAuthKey = authKey .trim ();
4850 this .apiVersion = options .apiVersion ;
4951 String serverUrl =
5052 (options .getServerUrl () != null )
5153 ? options .getServerUrl ()
52- : (isFreeAccountAuthKey (authKey ) ? DEEPL_SERVER_URL_FREE : DEEPL_SERVER_URL_PRO );
54+ : (isFreeAccountAuthKey (sanitizedAuthKey )
55+ ? DEEPL_SERVER_URL_FREE
56+ : DEEPL_SERVER_URL_PRO );
5357
5458 Map <String , String > headers = new HashMap <>();
5559 if (options .getHeaders () != null ) {
5660 headers .putAll (options .getHeaders ());
5761 }
58- headers .putIfAbsent ("Authorization" , "DeepL-Auth-Key " + authKey );
62+ headers .putIfAbsent ("Authorization" , "DeepL-Auth-Key " + sanitizedAuthKey );
5963 headers .putIfAbsent (
6064 "User-Agent" ,
6165 constructUserAgentString (options .getSendPlatformInfo (), options .getAppInfo ()));
Original file line number Diff line number Diff line change @@ -29,6 +29,16 @@ void testEmptyAuthKey() {
2929 });
3030 }
3131
32+ @ Test
33+ void testNullAuthKey () {
34+ IllegalArgumentException thrown =
35+ Assertions .assertThrows (
36+ IllegalArgumentException .class ,
37+ () -> {
38+ Translator translator = new Translator (null );
39+ });
40+ }
41+
3242 @ Test
3343 void testInvalidAuthKey () {
3444 String authKey = "invalid" ;
You can’t perform that action at this time.
0 commit comments