Skip to content

Commit 297185e

Browse files
committed
more tests
1 parent 48a59f1 commit 297185e

File tree

2 files changed

+361
-2
lines changed

2 files changed

+361
-2
lines changed

src/test/java/org/htmlunit/html/HtmlScript2Test.java

Lines changed: 359 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ public void addEventListener_NoContent() throws Exception {
561561
+ " var s1 = document.createElement('script');\n"
562562
+ " s1.src = '" + scriptUrl + "';\n"
563563
+ " s1.addEventListener('load', function() { log('load'); }, false);\n"
564-
+ " s1.addEventListener('error', function() { logEx(e); }, false);\n"
564+
+ " s1.addEventListener('error', function(event) { log(event.type + ' ' + event.target); }, false);\n"
565565
+ " document.body.insertBefore(s1, document.body.firstChild);\n"
566566
+ " }\n"
567567
+ "</script>\n"
@@ -574,6 +574,364 @@ public void addEventListener_NoContent() throws Exception {
574574
loadPageVerifyTitle2(html);
575575
}
576576

577+
/**
578+
* @throws Exception on test failure
579+
*/
580+
@Test
581+
@Alerts("error [object HTMLScriptElement]")
582+
public void addEventListener_BadRequest() throws Exception {
583+
addEventListener(HttpStatus.BAD_REQUEST_400);
584+
}
585+
586+
/**
587+
* @throws Exception on test failure
588+
*/
589+
@Test
590+
@Alerts("error [object HTMLScriptElement]")
591+
public void addEventListener_Forbidden() throws Exception {
592+
addEventListener(HttpStatus.FORBIDDEN_403);
593+
}
594+
595+
/**
596+
* @throws Exception on test failure
597+
*/
598+
@Test
599+
@Alerts("error [object HTMLScriptElement]")
600+
public void addEventListener_NotFound() throws Exception {
601+
addEventListener(HttpStatus.NOT_FOUND_404);
602+
}
603+
604+
/**
605+
* @throws Exception on test failure
606+
*/
607+
@Test
608+
@Alerts("error [object HTMLScriptElement]")
609+
public void addEventListener_MethodNotAllowed() throws Exception {
610+
addEventListener(HttpStatus.METHOD_NOT_ALLOWED_405);
611+
}
612+
613+
/**
614+
* @throws Exception on test failure
615+
*/
616+
@Test
617+
@Alerts("error [object HTMLScriptElement]")
618+
public void addEventListener_NotAcceptable() throws Exception {
619+
addEventListener(HttpStatus.NOT_ACCEPTABLE_406);
620+
}
621+
622+
/**
623+
* @throws Exception on test failure
624+
*/
625+
@Test
626+
@Alerts("error [object HTMLScriptElement]")
627+
public void addEventListener_ProxyAuthRequired() throws Exception {
628+
addEventListener(HttpStatus.PROXY_AUTHENTICATION_REQUIRED_407);
629+
}
630+
631+
/**
632+
* @throws Exception on test failure
633+
*/
634+
@Test
635+
@Alerts("error [object HTMLScriptElement]")
636+
public void addEventListener_RequestTimeout() throws Exception {
637+
addEventListener(HttpStatus.REQUEST_TIMEOUT_408);
638+
}
639+
640+
/**
641+
* @throws Exception on test failure
642+
*/
643+
@Test
644+
@Alerts("error [object HTMLScriptElement]")
645+
public void addEventListener_Conflict() throws Exception {
646+
addEventListener(HttpStatus.CONFLICT_409);
647+
}
648+
649+
/**
650+
* @throws Exception on test failure
651+
*/
652+
@Test
653+
@Alerts("error [object HTMLScriptElement]")
654+
public void addEventListener_Gone() throws Exception {
655+
addEventListener(HttpStatus.GONE_410);
656+
}
657+
658+
/**
659+
* @throws Exception on test failure
660+
*/
661+
@Test
662+
@Alerts("error [object HTMLScriptElement]")
663+
public void addEventListener_LengthRequired() throws Exception {
664+
addEventListener(HttpStatus.LENGTH_REQUIRED_411);
665+
}
666+
667+
/**
668+
* @throws Exception on test failure
669+
*/
670+
@Test
671+
@Alerts("error [object HTMLScriptElement]")
672+
public void addEventListener_PreconditionFailed() throws Exception {
673+
addEventListener(HttpStatus.PRECONDITION_FAILED_412);
674+
}
675+
676+
/**
677+
* @throws Exception on test failure
678+
*/
679+
@Test
680+
@Alerts("error [object HTMLScriptElement]")
681+
public void addEventListener_PayloadTooLarge() throws Exception {
682+
addEventListener(HttpStatus.PAYLOAD_TOO_LARGE_413);
683+
}
684+
685+
/**
686+
* @throws Exception on test failure
687+
*/
688+
@Test
689+
@Alerts("error [object HTMLScriptElement]")
690+
public void addEventListener_UriTooLong() throws Exception {
691+
addEventListener(HttpStatus.URI_TOO_LONG_414);
692+
}
693+
694+
/**
695+
* @throws Exception on test failure
696+
*/
697+
@Test
698+
@Alerts("error [object HTMLScriptElement]")
699+
public void addEventListener_UnsupportedMediaType() throws Exception {
700+
addEventListener(HttpStatus.UNSUPPORTED_MEDIA_TYPE_415);
701+
}
702+
703+
/**
704+
* @throws Exception on test failure
705+
*/
706+
@Test
707+
@Alerts("error [object HTMLScriptElement]")
708+
public void addEventListener_RangeNotSatisfiable() throws Exception {
709+
addEventListener(HttpStatus.RANGE_NOT_SATISFIABLE_416);
710+
}
711+
712+
/**
713+
* @throws Exception on test failure
714+
*/
715+
@Test
716+
@Alerts("error [object HTMLScriptElement]")
717+
public void addEventListener_ExpectationFailed() throws Exception {
718+
addEventListener(HttpStatus.EXPECTATION_FAILED_417);
719+
}
720+
721+
/**
722+
* @throws Exception on test failure
723+
*/
724+
@Test
725+
@Alerts("error [object HTMLScriptElement]")
726+
public void addEventListener_ImaTeapot() throws Exception {
727+
addEventListener(HttpStatus.IM_A_TEAPOT_418);
728+
}
729+
730+
/**
731+
* @throws Exception on test failure
732+
*/
733+
@Test
734+
@Alerts("error [object HTMLScriptElement]")
735+
public void addEventListener_EnhanceYourCalm() throws Exception {
736+
addEventListener(HttpStatus.ENHANCE_YOUR_CALM_420);
737+
}
738+
739+
/**
740+
* @throws Exception on test failure
741+
*/
742+
@Test
743+
@Alerts("error [object HTMLScriptElement]")
744+
public void addEventListener_MisdirectedRequest() throws Exception {
745+
addEventListener(HttpStatus.MISDIRECTED_REQUEST_421);
746+
}
747+
748+
/**
749+
* @throws Exception on test failure
750+
*/
751+
@Test
752+
@Alerts("error [object HTMLScriptElement]")
753+
public void addEventListener_UnprocessableEntity() throws Exception {
754+
addEventListener(HttpStatus.UNPROCESSABLE_ENTITY_422);
755+
}
756+
757+
/**
758+
* @throws Exception on test failure
759+
*/
760+
@Test
761+
@Alerts("error [object HTMLScriptElement]")
762+
public void addEventListener_Locked() throws Exception {
763+
addEventListener(HttpStatus.LOCKED_423);
764+
}
765+
766+
/**
767+
* @throws Exception on test failure
768+
*/
769+
@Test
770+
@Alerts("error [object HTMLScriptElement]")
771+
public void addEventListener_FailedDependency() throws Exception {
772+
addEventListener(HttpStatus.FAILED_DEPENDENCY_424);
773+
}
774+
775+
/**
776+
* @throws Exception on test failure
777+
*/
778+
@Test
779+
@Alerts("error [object HTMLScriptElement]")
780+
public void addEventListener_UpgradeRequired() throws Exception {
781+
addEventListener(HttpStatus.UPGRADE_REQUIRED_426);
782+
}
783+
784+
/**
785+
* @throws Exception on test failure
786+
*/
787+
@Test
788+
@Alerts("error [object HTMLScriptElement]")
789+
public void addEventListener_PreconditionRequired() throws Exception {
790+
addEventListener(HttpStatus.PRECONDITION_REQUIRED_428);
791+
}
792+
793+
/**
794+
* @throws Exception on test failure
795+
*/
796+
@Test
797+
@Alerts("error [object HTMLScriptElement]")
798+
public void addEventListener_TooManyRedirects() throws Exception {
799+
addEventListener(HttpStatus.TOO_MANY_REQUESTS_429);
800+
}
801+
802+
/**
803+
* @throws Exception on test failure
804+
*/
805+
@Test
806+
@Alerts("error [object HTMLScriptElement]")
807+
public void addEventListener_RequestHeaderFieldsTooLarge() throws Exception {
808+
addEventListener(HttpStatus.REQUEST_HEADER_FIELDS_TOO_LARGE_431);
809+
}
810+
811+
/**
812+
* @throws Exception on test failure
813+
*/
814+
@Test
815+
@Alerts("error [object HTMLScriptElement]")
816+
public void addEventListener_UnavailableForLegalReasons() throws Exception {
817+
addEventListener(HttpStatus.UNAVAILABLE_FOR_LEGAL_REASONS_451);
818+
}
819+
820+
/**
821+
* @throws Exception on test failure
822+
*/
823+
@Test
824+
@Alerts("error [object HTMLScriptElement]")
825+
public void addEventListener_InternalServerError() throws Exception {
826+
addEventListener(HttpStatus.INTERNAL_SERVER_ERROR_500);
827+
}
828+
829+
/**
830+
* @throws Exception on test failure
831+
*/
832+
@Test
833+
@Alerts("error [object HTMLScriptElement]")
834+
public void addEventListener_NotImplemented() throws Exception {
835+
addEventListener(HttpStatus.NOT_IMPLEMENTED_501);
836+
}
837+
838+
/**
839+
* @throws Exception on test failure
840+
*/
841+
@Test
842+
@Alerts("error [object HTMLScriptElement]")
843+
public void addEventListener_BadGateway() throws Exception {
844+
addEventListener(HttpStatus.BAD_GATEWAY_502);
845+
}
846+
847+
/**
848+
* @throws Exception on test failure
849+
*/
850+
@Test
851+
@Alerts("error [object HTMLScriptElement]")
852+
public void addEventListener_ServiceUnavailable() throws Exception {
853+
addEventListener(HttpStatus.SERVICE_UNAVAILABLE_503);
854+
}
855+
856+
/**
857+
* @throws Exception on test failure
858+
*/
859+
@Test
860+
@Alerts("error [object HTMLScriptElement]")
861+
public void addEventListener_GatewayTimeout() throws Exception {
862+
addEventListener(HttpStatus.GATEWAY_TIMEOUT_504);
863+
}
864+
865+
/**
866+
* @throws Exception on test failure
867+
*/
868+
@Test
869+
@Alerts("error [object HTMLScriptElement]")
870+
public void addEventListener_HttpVersionNotSupported() throws Exception {
871+
addEventListener(HttpStatus.HTTP_VERSION_NOT_SUPPORTED_505);
872+
}
873+
874+
/**
875+
* @throws Exception on test failure
876+
*/
877+
@Test
878+
@Alerts("error [object HTMLScriptElement]")
879+
public void addEventListener_InsufficientStrorage() throws Exception {
880+
addEventListener(HttpStatus.INSUFFICIENT_STORAGE_507);
881+
}
882+
883+
/**
884+
* @throws Exception on test failure
885+
*/
886+
@Test
887+
@Alerts("error [object HTMLScriptElement]")
888+
public void addEventListener_LoopDetected() throws Exception {
889+
addEventListener(HttpStatus.LOOP_DETECTED_508);
890+
}
891+
892+
/**
893+
* @throws Exception on test failure
894+
*/
895+
@Test
896+
@Alerts("error [object HTMLScriptElement]")
897+
public void addEventListener_NotExtended() throws Exception {
898+
addEventListener(HttpStatus.NOT_EXTENDED_510);
899+
}
900+
901+
/**
902+
* @throws Exception on test failure
903+
*/
904+
@Test
905+
@Alerts("error [object HTMLScriptElement]")
906+
public void addEventListener_NetworkAuthenticationRequired() throws Exception {
907+
addEventListener(HttpStatus.NETWORK_AUTHENTICATION_REQUIRED_511);
908+
}
909+
910+
private void addEventListener(final int statusCode) throws Exception {
911+
// use always a different url to avoid caching effects
912+
final URL scriptUrl = new URL(URL_SECOND, "" + System.currentTimeMillis() + ".js");
913+
914+
final String html
915+
= "<html><head>\n"
916+
+ "<script>\n"
917+
+ LOG_TITLE_FUNCTION
918+
+ " function test() {\n"
919+
+ " var s1 = document.createElement('script');\n"
920+
+ " s1.src = '" + scriptUrl + "';\n"
921+
+ " s1.addEventListener('load', function() { log('load'); }, false);\n"
922+
+ " s1.addEventListener('error', function(event) { log(event.type + ' ' + event.target); }, false);\n"
923+
+ " document.body.insertBefore(s1, document.body.firstChild);\n"
924+
+ " }\n"
925+
+ "</script>\n"
926+
+ "</head>\n"
927+
+ "<body onload='test()'></body>\n"
928+
+ "</html>";
929+
930+
getMockWebConnection().setResponse(scriptUrl, (String) null,
931+
statusCode, "test", MimeType.TEXT_JAVASCRIPT, null);
932+
loadPageVerifyTitle2(html);
933+
}
934+
577935
/**
578936
* Regression test for bug #1267.
579937
* @throws Exception if an error occurs

src/test/java/org/htmlunit/html/HtmlScriptTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ private void addEventListener_error(final boolean throwOnFailingStatusCode) thro
218218
+ "</html>";
219219
final WebClient client = getWebClient();
220220
client.getOptions().setThrowExceptionOnFailingStatusCode(throwOnFailingStatusCode);
221+
client.getOptions().setThrowExceptionOnScriptError(false);
222+
221223
final MockWebConnection conn = new MockWebConnection();
222224
conn.setResponse(URL_FIRST, html);
223225
conn.setResponse(URL_SECOND, "var foo;", MimeType.TEXT_JAVASCRIPT);
@@ -226,7 +228,6 @@ private void addEventListener_error(final boolean throwOnFailingStatusCode) thro
226228
client.setWebConnection(conn);
227229
final List<String> actual = new ArrayList<>();
228230
client.setAlertHandler(new CollectingAlertHandler(actual));
229-
client.getOptions().setThrowExceptionOnScriptError(false);
230231
client.getPage(URL_FIRST);
231232
assertEquals(getExpectedAlerts(), actual);
232233
}

0 commit comments

Comments
 (0)